Local Functions In C# 7.0

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

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/
http://www.c-sharpcorner.com/article/local-functions-in-c-sharp-7-0/

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *