With an example explain Local functions of C# 7.0

 Posted by Rajnilari2015 on 6/23/2016 | Category: C# Interview questions | Views: 2003 | Points: 40
Answer:

Local functions allows us to define a function inside the scope/block of another function. It allows to declare methods and types in block scope just like variables. The concept is very similar to Closure.
e.g.

using System;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
void AddNumbersUsingLocalFunction(int a=10, int b=20)
{
Console.WriteLine("Using Local Function : Sum is {0}", a+b);

}
AddNumbersUsingLocalFunction(16);
Console.ReadKey();
}
}
}


As can be figure out that, inside the Main method we have implemented the AddNumbersUsingLocalFunction function. Hence it's scope is bounded inside that block only.


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response