A local function is defined as a nested method inside a containing member. Local functions are private
methods of a type that are nested in another member.
In other words, a local function is a private function of a function whose scope is limited to that function in which it is created. It can be declared in and called from any of the following.
Local functions make the intent of your code clear. Anyone reading your code can see that the method is not callable except by the containing method. You can declare the local functions using the following syntax.
<modifiers> <return-type> <method-name> <parameter-list>
All local variables that are defined in the containing member, including its method parameters, are accessible in a non-static local function. An access modifier can not be included in a local function definition because all local functions are private
.
Including an access modifier, such as the private keyword, generates the following compiler error.
Error CS0106, "The modifier 'private' is not valid for this item."
Let's consider the following simple example of a local function where a local function WriteMessage
is defined that is private
to a SimpleLoalFunctionExample
method.
public static void SimpleLoalFunctionExample()
{
WriteMessage("Hello!");
WriteMessage("You are calling");
WriteMessage("a local function");
void WriteMessage(string message)
{
Console.WriteLine("The message is: {0}", message);
}
}
It is the simplest form of a local function in which a WriteMessage
function takes a string
as a parameter and prints that message to the console.
Local functions can access variables from the enclosing scope of the containing method, as shown in the below example.
public static void AddExampleUsingLocalFunction()
{
int num1 = 34;
int num2 = 21;
Console.WriteLine(AddNumbers());
int AddNumbers()
{
return num1 + num2;
}
}
As you can see, the local functions automatically capture the enclosing context scope to make any variables from the containing method available inside them. The local variables called num1
and num2
are accessible for the function as they are defined inside the container function.