Nested Methods in C#
In this article, you will see how to define nested methods (nested functions) in C# with the help of various examples.
What is a Nested Method?
A nested method is a method that is defined inside another method. A nested method is also called a local method or a local function.
C# allows you to define as many nested methods as you want inside another level. Also, you can define a nested method inside another nested method and you can go down to virtually unlimited levels while defining a nested method.
Defining Nested Methods
Nested methods are defined just like ordinary methods albeit inside another method. Here is an example:
class Program { int c = 50; static void Main(string[] args) { int a = 10; int z = myNestedMethod(20, 30); // Accessing Nested Method Console.WriteLine(z); int myNestedMethod(int x, int y) { int result = x + y + a; // accessing variable a return result; } Console.ReadKey(); } }
In the above script, you have a normal method called Main. Inside the main method, you define a nested method called myNestedMethod.
The nested method myNestedMethod accepts two parameter values x and y. Inside the nested method body, the parameter values x and y, and the variable c which is defined inside the Main method is added and the result of this addition is returned to the function call. Since the variable c contains 10, and the values passed to the function call to the method myNestedMethod are 20, and 30, you will see 60 printed in the output.
Since a nested method can only be called inside the method in which it is defined, you do not have to specify any access modifier i.n. public, private, protected, etc, for the nested method. For example, in the above script myNestedMethod can only be called from inside the Main method.
Also, the nested method can access variables defined inside a method in which it is defined. For instance, in our example code, the nested method myNestedMethod can access the variable c defined inside the Main method.
Nesting a Method inside Another Nested Method
As discussed earlier, you can nest a method inside another nested method which can be nested inside another nested method, and so on. You can have any level of hierarchy for nested methods as you like. Let’s see an example of nesting a method inside another nested method.
class Program { static void Main(string[] args) { int z = myNestedMethod(5, 4); // Accessing Nested Method Console.WriteLine(z); int myNestedMethod(int x, int y) { int result = x + y; // accessing variable a int square = myNewNestedMethod(result, 2); int myNewNestedMethod(int num1, int num2) { int result2 = Convert.ToInt32(Math.Pow(num1, num2)); return result2; } return square; } Console.ReadKey(); } }
In the script above, you have three methods: Main, myNestedMethod, and myNewNestedMethod. The method myNestedMethod is nested inside the main method while the method myNewNestedMethod is defined inside the method myNestedMethod.
The top-level nested method myNested method accepts two parameter values, sums them, and then passes the result of the summation and another value to the second level nested method i.e. myNewNestedMethod.
The myNewNestedMethod takes the power of the first parameter passed to it with respect to the second parameter value and returns the result.
Let’s see the workflow of the above script. Inside the main method, the myNestedMethod is called using parameter values 5 and 4. Inside the myNestedMethod, the values 5 and 4 are added and the result i.e. 9 is stored in the result parameter.
The myNestedMethod then passes the result variable and the integer 2 to the myNewNestedMethod which raises 9 to the power of 2 and returns the result2 variable (containing 81) to the myNestedMethod, which returns this value to the Main method. The output i.e. 81 is then displayed on the console.
Other useful articles:
- TOP-10 Basic C# Interview Questions and Answers
- TOP-10 C# Questions for Advanced Programmers
- 10 Important C# Interview Questions and Answers
- Most Asked C# Interview Q & A
- C# Interview for Beginners
- TOP-10 Advanced C# Interview Questions and Answers
- Introduction to Functions in C#
- C# Jagged Arrays
- C Sharp Interview Questions for 5 Years Experience
- C Sharp Interview Questions for Testers
- C Sharp Questions for 10 Years Experience
- Iteration Statements in C#
- Exception Handling in C#
- C Sharp Methods
- C# Data Types
- C# Vs. C++
- Regular Expressions in C Sharp
- Defining Nested Methods in C Sharp
- C Sharp Dynamic Data Type
- C Custom Exceptions
- How to Generate Random Numbers with C Sharp
- Pass by Value vs Pass by Reference in C Sharp