Can We Create Method Inside Method In C#?

When writing code in C# or learning about methods in programming, you may be wondering if we can create a method inside another method in C#. In this blog, I discuss how we can create a method inside another method in a C# program.
 

Introduction

 
Creating a method inside another method is called local functions in C#.
  1. Using a method inside another method makes code easier to read. Local functions is just like lamda expressions.
  2. Local function is a private method.
  3. We can declare local functions inside methods, constructors, anonymous methods, lambda expressions and other local functions.
  4. We can write a local function as generic.
  5. We can use out and ref parameters in location functions.
  6. We can use params in local functions.
  7. We can create multiple local functions inside a method.
  8. Local functions do not allow access modifiers
Local Function Syntax
  1. <modifiers> <return-type> <method-name> <parameter-list>
Example 1: Multiple Local Functions
 
Below example provides information has to ow we can create multiple local functions. Here the Main is the method and inside that we have created Sum, Sub, Mul local functions.
  1. using System;  
  2. namespace LocalFunctions  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             Console.WriteLine($"Sum: {Sum(10, 20)}");  
  9.             Console.WriteLine($"Sub: {Sub(10, 20)}");  
  10.             Console.WriteLine($"Mul: {Mul(10, 20)}");  
  11.             Console.WriteLine($"Sum: {Sum(5, 15)}");  
  12.   
  13.             int Sum(int a, int b)  
  14.             {  
  15.                 return a + b;  
  16.             }  
  17.             int Sub(int a, int b)  
  18.             {  
  19.                 return a - b;  
  20.             }  
  21.             int Mul(int a, int b)  
  22.             {  
  23.                 return a * b;  
  24.             }  
  25.             Console.ReadKey();  
  26.         }  
  27.     }  
  28. }   
Output
  1. Sum: 30
  2. Sub: -10
  3. Mul: 200
  4. Sum: 20
Example 1 - Generic Local Functions
 
The below example provides information on how we can create generic location functions. In this code snippet, we have local functions with generics.
  1. using System;  
  2. namespace LocalFunctions  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             Display<int>(Sum(10, 20));  
  9.             Display<string>("Local Functions Demo");  
  10.   
  11.             void Display<T>(T value)  
  12.             {  
  13.                 Console.WriteLine("Value is: " + value);  
  14.             }  
  15.             Console.ReadKey();  
  16.         }  
  17.     }  
  18. }  
Output
  1. Value is: 30
  2. Value is: Local Functions Demo
That's all for this blog. Thanks for reading my blog. I appreciate your feedback.
 
To learn more about local functions, check out Local Functions: Why and When to use them