A function inside the body of another function and is private, the scope of which is limited to the function within which it is created is called local function in C# using which a method can be declared inside the body of another method which is already defined and this local functions feature was introduced in C# in the C# version 7.0. and the type of function created inside the body of another function is same as the type of the function within which this function is created and such local functions can be called by the members of their container and more than one local function is allowed to be created but the usage of static keyword with the local functions is not permitted.
Syntax:
Given below is the syntax:
<modifiers: async | unsafe> <return-type> <method-name> <parameter-list>
Given below are the examples mentioned:
C# program to demonstrate local functions in a program to add two numbers.
Code:
using System; //a class called check is defined namespace LocalFunction { public class Program { // Main method is called public static void Main(string[] args) { // the local methods are being called within the main method int res = Addition(100, 200); Console.WriteLine("The addition result of adding 100 and 200 is: {0}", +res); //local method is created int Addition(int x, int y) { return x + y; } } } }
Output:
In the above program, a class called check is defined. Then the main method is called within which the local methods are defined. Then the local methods created within the main method is called with the two numbers to be added are passed as parameter to the local method.
C# program to demonstrate local functions in a program.
Code:
using System; //a class called program is called namespace LocalFunction { public class Program { //main method is called public static void Main(string[] args) { //Local Function is created int Function(int x) { return 100 * x; } //Calling the local function within the main method Console.WriteLine("The product after performing the operation is: {0}",Function(10)); } } }
Output:
In the above program, a class called program is defined. Then the main method is called within which the local method to find the product of the number after multiplying with 100, is passed as a parameter, is defined. Then the local method created within the main method is called with a number whose product is to be found after multiplying with 100 is passed as parameter to the local method.
C# program to demonstrate local functions in a program to find the square of a number.
Code:
using System; //a class called program is called namespace LocalFunction { public class Program { //main method is called public static void Main(string[] args) { //Local Function is created int Square(int x) { return x * x; } //Calling the local function within the main method Console.WriteLine("The square after performing the operation is: {0}",Square(10)); } } }
Output:
In the above program, a class called program is defined. Then the main method is called within which the local method to find the square of the number passed as a parameter is defined. Then the local method created within the main method is called with a number whose square is to be found is passed as parameter to the local method.
The above is the detailed content of C# Local Functions. For more information, please follow other related articles on the PHP Chinese website!