Updated April 12, 2023
Introduction to C# Local Functions
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>
- Where async and unsafe are the modifiers that can be used with the local method.
- return type is the type of the value returned by the method.
- method name is the name given to the method.
- parameter list is the list of parameters that can be passed to the method.
Working of Local Functions in C#
- Local functions are private functions which can be declared inside the body of another function and the scope of such local functions are restricted to the function within which it is created.
- Local functions can be called from finalizers, lambda expressions, property assessors, constructors etc.
- Access modifiers and static keyword cannot be used in local function, even the modifier private because local functions are by default private.
- Local variables which are defined inside the container methods and includes method parameters can be accessed using local functions.
- Attributes cannot be applied to the local functions or to its parameters and type of parameters.
- The modifiers unsafe and async can be used with local functions.
Examples
Given below are the examples mentioned:
Example #1
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.
Example #2
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.
Example #3
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.
Recommended Articles
This is a guide to C# Local Functions. Here we discuss the introduction, working of local functions in C# and programming examples. You may also have a look at the following articles to learn more –