Local Functions In C# 7.0

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.

  1. 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.
  2. 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 
  1. static void Main(string[] args)  
  2. {  
  3.     // Method calling  
  4.     GetMyName();  
  5.   
  6.     //Method Declaration  
  7.     void GetMyName()  
  8.     {  
  9.         Console.WriteLine( "My Name Is Omar Maher"  );  
  10.     }          
  11. }   

In the code mentioned above, we created a function GetMyName() inside Main Function.

Let's run it and see the result.

result

Also, you can make a calling for the local function after the declaration, as shown below. 

  1. static void Main(string[] args)  
  2. {  
  3.     //Method Declaration  
  4.     void GetMyName()  
  5.     {  
  6.         Console.WriteLine( "My Name Is Omar Maher"  );  
  7.     }  
  8.   
  9.     // Method calling  
  10.     GetMyName();  
  11. }   

It will give you the same result.

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. 

  1. static void Main(string[] args)  
  2. {  
  3.     // Method calling  
  4.     GetMyName("Omar Maher");  
  5.   
  6.     //Method Declaration  
  7.     void GetMyName(string name)  
  8.     {  
  9.         Console.WriteLine($"My Name Is {name}");  
  10.     }  
  11. }   

When you run, you will get the same result.

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. 

  1. static void Main(string[] args)  
  2. {  
  3.     string name = "Omar Maher";  
  4.     // Method calling  
  5.     GetMyName();  
  6.   
  7.     //Method Declaration  
  8.     void GetMyName()  
  9.     {  
  10.         Console.WriteLine($"My Name Is {name}");  
  11.     }  
  12. }   

In the example, the variable name is available inside GetMyName() and outside it.

The result will be the same

result

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. 

  1. class MyData  
  2. {  
  3.     public MyData()  
  4.     {  
  5.     }  
  6. }   

Now, let's create a local function GetMyName() inside the MyData Constructor, as shown. 

  1. class MyData  
  2. {  
  3.     public MyData()  
  4.     {  
  5.         // Method calling  
  6.         GetMyName();  
  7.   
  8.         //Method Declaration  
  9.         void GetMyName()  
  10.         {  
  11.             Console.WriteLine($"My Name Is Omar Maher");  
  12.         }  
  13.     }  
  14. }   

Let's create an object from MyData Class in Main Method, as shown. 

  1. static void Main(string[] args)  
  2. {  
  3.     MyData data = new MyData();  
  4. }   

When we run the code, the Constructor will call GetMyName() and will continue.

The result will be the same

result

Example 5

Also, you can send the parameters and throw the Constructor, as shown. 

  1. class MyData  
  2. {  
  3.     public MyData(string name)  
  4.     {  
  5.         // Method calling  
  6.         GetMyName();  
  7.   
  8.         //Method Declaration  
  9.         void GetMyName()  
  10.         {  
  11.             Console.WriteLine($"My Name Is {name}");  
  12.         }  
  13.     }  
  14. }   

Now, name the variable and it will be available anywhere inside the Constructor.

Let's create another object. 

  1. static void Main(string[] args)  
  2. {  
  3.     MyData data1 = new MyData("Omar Maher");  
  4. }   

The result will be the same

result

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.

Up Next
    Ebook Download
    View all
    Learn
    View all