Definition
Local functions means you can create a function within anther function and it encapsulates completely within that function.
Features
Parameters and local variable from the enclosing scope are visible within the local function.
Why I should think in Local functions?
Let's think about when you need a helper function, which will be called once.
In the old days, before C# 7, you could think in 2 ways.
- Create a private method inside the class and call it in the function but that will end up having many private methods, which wouldn’t be reused.
- Using Func and Actions types with the anonymous methods but this solution has a limitation like you can’t use generics , out , ref and params.
Now, in C# 7 world, you can declare a function inside anther one.
Let's take some examples.
Example 1
- static void Main(string[] args)
- {
-
- GetMyName();
-
-
- void GetMyName()
- {
- Console.WriteLine( "My Name Is Omar Maher" );
- }
- }
In the code mentioned above, we created a function GetMyName() inside Main Function.
Let's run it and see the result.
Also, you can make a calling for the local function after the declaration, as shown below.
- static void Main(string[] args)
- {
-
- void GetMyName()
- {
- Console.WriteLine( "My Name Is Omar Maher" );
- }
-
-
- GetMyName();
- }
It will give you the same result.
Example 2
Also, you can declare the parameter into the local function but it will be available in the scope of GetMyName(string name), as shown.
- static void Main(string[] args)
- {
-
- GetMyName("Omar Maher");
-
-
- void GetMyName(string name)
- {
- Console.WriteLine($"My Name Is {name}");
- }
- }
When you run, you will get the same result.
Example 3
Another example is that if you want the variable available anywhere in the Main method; you can declare the variable outside the local function, as shown.
- static void Main(string[] args)
- {
- string name = "Omar Maher";
-
- GetMyName();
-
-
- void GetMyName()
- {
- Console.WriteLine($"My Name Is {name}");
- }
- }
In the example, the variable name is available inside GetMyName() and outside it.
The result will be the same
Example 4
Also, you can use the local functions inside the constrictors.
Let's create a class MyData and create a Constructor inside it, as shown.
- class MyData
- {
- public MyData()
- {
- }
- }
Now, let's create a local function GetMyName() inside the MyData Constructor, as shown.
- class MyData
- {
- public MyData()
- {
-
- GetMyName();
-
-
- void GetMyName()
- {
- Console.WriteLine($"My Name Is Omar Maher");
- }
- }
- }
Let's create an object from MyData Class in Main Method, as shown.
- static void Main(string[] args)
- {
- MyData data = new MyData();
- }
When we run the code, the Constructor will call GetMyName() and will continue.
The result will be the same
Example 5
Also, you can send the parameters and throw the Constructor, as shown.
- class MyData
- {
- public MyData(string name)
- {
-
- GetMyName();
-
-
- void GetMyName()
- {
- Console.WriteLine($"My Name Is {name}");
- }
- }
- }
Now, name the variable and it will be available anywhere inside the Constructor.
Let's create another object.
- static void Main(string[] args)
- {
- MyData data1 = new MyData("Omar Maher");
- }
The result will be the same
You can find the source code here,
https://github.com/omarmaher100/LocalFunctions
In the end, I hope it will help you. Wait for me for more topics.