What are Local Functions in C# 7.0?
The local function is an ability to declare methods inside an already defined function’s body. We are able to write local functions in C# 6.0 too but they can’t have a complete body or function name, and can only be anonymous.
Why Local Functions in C# 7.0?
As per the Microsoft blog, this is already (kind of) possible by using the Func and Action types with anonymous methods.

However, they lack a few features,
- Generics
- ref and out parameters
- params
How to write Local Functions in C# 7.0
Local functions have the same capabilities as normal methods but will only be scoped to the block they were declared in.
Now, let’s try to write a normal local function first.
In the following image of code, you can check that I just created a function with two argument types (int, string) and it has all the behavior as a normal function. Since it is a local function, you can use or call it within the same function only.

Code
- static void Main(string[] args)
- {
- string myFunc(int p1,string p2)
- {
- return p2.Substring(p1);
- }
- Console.WriteLine(myFunc(2,"Nitin Pandit"));
- Console.Read();
- }




BablooPosted Aug 23, 2019, 6:27 AM
Very nice sir thank you for shairing....
kailash urmliyaPosted Jan 16, 2017, 12:56 AM
Good One... great article sir ji
Manoj KallaPosted Dec 29, 2016, 6:48 AM
Good one...Thank you...Sir ji