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.
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:
Radius of the circle: 30 Area of circle: 2826 Circumference of the circle is: 188.4
Error CS8421: A static local function cannot contain a reference to 'a'. (CS8421) (f)
- C# | Difference between Static Constructors and Non-Static Constructors
- Local Function in C#
- C# | Implicitly Typed Local Variables - var
- C# | Static Class
- Static keyword in C#
- Hash Function for String data in C#
- Aggregation Function in LINQ
- Differences Between .NET Core and .NET Framework
- C# - Indexers Using String as an Index
- Different Types of HTML Helpers in ASP.NET MVC
- Difference Between .NET and ASP.NET Framework
- Difference Between Properties and Indexers in C#
- C# Coding Standards
- C# Program to Convert a Binary String to an Integer
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.