Static Local Function in C# 8.0

In C# 7.0, local functions are introduced. The local function allows you to declare a method inside the body of an already defined method. Or in other words, we can say that a local function is a private function of a function whose scope is limited to that function in which it is created. The type of local function is similar to the type of function in which it is defined. You can only call the local function from their container members.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple C# program to
// illustrate local function
using System;
  
class Program {
  
    // Main method
    public static void Main()
    {
        // Here SubValue is the local
        // function of the main function
        void SubValue(int a, int b)
        {
            Console.WriteLine("Value of a is: " + a);
            Console.WriteLine("Value of b is: " + b);
            Console.WriteLine("final result: {0}", a - b);
            Console.WriteLine();
        }
  
        // Calling Local function
        SubValue(30, 10);
        SubValue(80, 60);
    }
}

chevron_right


Output:



Value of a is: 30
Value of b is: 10
final result: 20

Value of a is: 80
Value of b is: 60
final result: 20

But in C# 7.0 you are not allowed to use static modifier with local function or in other words you are not allowed to create a static local function. This feature is added in C# 8.0. In C# 8.0, you are allowed to use a static modifier with the local function. This ensures that the static local function does not reference any variable from the enclosing or surrounding scope. If static local function tries to access the variable from the enclosed scope, then the compiler will throw an error. Let us discuss this concept with the help of the given examples:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple C# program to illustrate 
// the static local function
using System;
  
class Program {
  
    // Main method
    public static void Main()
    {
        // Here AreaofCircle is the local
        // function of the main function
        void AreaofCircle(double a)
        {
            double ar;
            Console.WriteLine("Radius of the circle: " + a);
  
            ar = 3.14 * a * a;
  
            Console.WriteLine("Area of circle: " + ar);
  
            // Calling static local function
            circumference(a);
  
            // Circumference is the Static local function
            static void circumference(double radii)
            {
                double cr;
                cr = 2 * 3.14 * radii;
                Console.WriteLine("Circumference of the circle is: " + cr);
            }
        }
  
        // Calling function
        AreaofCircle(30);
    }
}

chevron_right


Output:

Radius of the circle: 30
Area of circle: 2826
Circumference of the circle is: 188.4

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Simple C# program to illustrate
// the static local function
using System;
  
class Program {
  
    // Main method
    public static void Main()
    {
        // Here AreaofCircle is the local
        // the function of the main function
        void AreaofCircle(double a)
        {
            double ar;
            Console.WriteLine("Radiius of the circle: " + a);
  
            ar = 3.14 * a * a;
  
            Console.WriteLine("Area of circle: " + ar);
  
            // Circumference is the Static local function
            // If circumferenc() try to access the enclosing 
            // scope variable, then the compile will give error
            static void circumference()
            {
                double cr;
                cr = 2 * 3.14 * a;
                Console.WriteLine("Circumference of the circle is: " + cr);
            }
        }
  
        // Calling function
        AreaofCircle(30);
    }
}

chevron_right


Output:

Error CS8421: A static local function cannot contain a reference to 'a'. (CS8421) (f)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.