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.
- static void Main(string[] args)
- {
-
- int Add(int a, int b)
- {
- return a + b;
- }
-
- Console.WriteLine(Add(3,4));
- Console.ReadKey();
- }
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.
References
References used to write this article,
https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/