Local Functions In C#

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. {  
  3.   
  4.   int Add(int a, int b)  
  5.   {  
  6.     return a + b;  
  7.   }  
  8.   
  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. 

Summary

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

References used to write this article,

https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.