Local Functions In C# 7.0

Introduction 

 
Local functions are a new feature in C# 7 that allows defining a function inside another function.
 
Local functions are similar to anonymous methods. Sometimes it's not necessary to create a named function to perform a specific action because the functionality is only local to a specific function, and a named function would only pollute the outer scope
 
The main goal of Local Functions is encapsulation so the compiler is enforcing that such functions cannot be called from anywhere else in the class. Consider the below example:
  1. class SampleLocalFunction {  
  2.     {  
  3.         get {  
  4.             return $ @ "{GetFirstName()} {GetLastName()}";  
  5.             string GetFirstName() => "Prasad";  
  6.             string GetLastName() => "Raveendran";  
  7.         }  
  8.     }  
  9. }  
Here, we have a getter property, which is composing a FullName comprised of two different sections. Each section is being retrieved through separate private methods. We have encapsulated methods usage by Local Functions.
 
Common places where local functions are defined:
  1. Methods
  2. Constructors
  3. Property accessors
  4. Event accessor
  5. Lambda expressions
  6. Finalizers, a.k.a destructors
  7. Local functions (meaning local functions can be nested within each other)

Generic Local Function

 
Generic functions combine reusability, type safety, and efficiency. They are often used with collections and methods, which operate on them.
 
Example below:
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         void MyGeneric < Value > (Value x) {  
  4.             Console.WriteLine($ "Value from generic message: {x}");  
  5.         }  
  6.         MyGeneric < string > ("Next Year");  
  7.         MyGeneric < int > (2021);  
  8.         Console.ReadKey();  
  9.     }  
  10. }  
Output
 
Value from the generic message: Next Year
 
Value from the generic message: 2021
 
You can also reference out parameters in local functions that are declared in the same scope
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         void MyOut(string x, out string s) {  
  4.             s = $ "Value from generic message using out parameter: {x}";  
  5.         }  
  6.         string message = null;  
  7.         MyOut("Next Year is 2021"out message);  
  8.         Console.WriteLine($ "The value of out message: {message}");  
  9.         Console.ReadKey();  
  10.     }  
  11. }  
Output
 
The value of our message: Value from the generic message using the out parameter: The Next Year is 2021.
Local Functions can be used with params.
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         void MyParams(params int[] array) {  
  4.             foreach(int element in array) {  
  5.                 Console.WriteLine($ "Display the elements from paramArray: {element}");  
  6.             }  
  7.         }  
  8.         int[] paramArray = new int[] {  
  9.             1,  
  10.             3,  
  11.             5,  
  12.             7,  
  13.             9,  
  14.             11,  
  15.             13  
  16.         };  
  17.         MyParams(paramArray);  
  18.         Console.ReadKey();  
  19.     }  
  20. }  
Output
 
Display the elements from paramArray: 1
Display the elements from paramArray: 3
Display the elements from paramArray: 5
Display the elements from paramArray: 7
Display the elements from paramArray: 9
Display the elements from paramArray: 11
Display the elements from paramArray: 13
 
We inspected use cases for local functions in C#. I hope this has been informative to you and you have found what you were looking for.


Next Recommended Reading C# 7.0 New Feature - Tuples In C# 7