CSharp - Method Local Method

Introduction

You can define a method inside another method:

void Test()
{
       Console.WriteLine (Test (3));
       Console.WriteLine (Test (4));
       Console.WriteLine (Test (5));

       int Test (int value) => value * value * value;
}

The local method Test(int value) is visible only to the enclosing method.

Local methods can access the local variables and parameters of the enclosing method.

Local methods can be inside property accessors, constructors.

Local methods can have inner local methods.

Lambda expressions can have local methods.

Local methods can be iterators or asynchronous.

The static modifier is invalid for local methods.

Local methods are implicitly static if the enclosing method is static.

Demo

using System;
class MainClass//from www .ja  v a  2s  . co  m
{
   public static void Main(string[] args)
   {
       Console.WriteLine (Test (3));
       Console.WriteLine (Test (4));
       Console.WriteLine (Test (5));

       int Test (int value) => value * value * value;

   }
}

Result