Local Functions

C# 7.0 adds a new feature called local functions, which allows local methods to be defined and called within a method. This wasn’t possible in the previous versions. The following code snippet defines a method named "Add" inside the "Main" method and calls within the "Main" method.

  1. static void Main(string[] args)  
  2. {  
  4.   int Add(int a, int b)  
  5.   {  
  6.     return a + b;  
  7.   }  
  9.   Console.WriteLine(Add(3,4));  
  10.   Console.ReadKey();  
  11. }   

As its name suggests, a local function is available to the method it is defined inside only. 


Local functions is a new concept introduced in C# 7.0. In this article, we learned how to write and use a local function. 

Next C# 7.0 Feature - Literal Improvements in C# 7.0  


References used to write this article,

Mahesh Chand
1 341.8k 225.9m
Next » Deconstruction In C#