Local functions
Local functions are functions defined inside other methods. A local function usually contains actions that are applied only within its method.
For example, let's define a method that compares the sum of numbers in two arrays:
void Compare(int[] numbers1, int[] numbers2)
{
int numbers1Sum = 0;
int numbers2Sum = 0;
foreach(int number in numbers1)
numbers1Sum += number;
foreach (int number in numbers2)
numbers2Sum += number;
if (numbers1Sum > numbers2Sum)
Console.WriteLine("the sum of numbers from the numbers1 array is greater");
else if (numbers1Sum < numbers2Sum)
Console.WriteLine("the sum of numbers from the numbers2 array is greater");
else
Console.WriteLine("the sums of numbers in both arrays are equal");
}
int[] numbers1 = { 1, 2, 3 };
int[] numbers2 = { 3, 4, 5, 6, 7 };
Compare(numbers1, numbers2);
Here the Compare method takes two arrays and sequentially calculates the sum of their elements to find out which array has the greater sum. Although everything works, there is one drawback here: the actions that calculate the sum of an array are repeated:
int numbers1Sum = 0;
foreach(int number in numbers1)
numbers1Sum += number;
Moreover, what if we want to compare only the sum of positive or even numbers, or somehow change the comparison logic? It is better to extract the repeated actions into a separate method. However, if these actions are not called anywhere else in the program and are used only in one method, then it makes sense to define these actions as a local function. To do this, let's modify the Compare method as follows:
void Compare(int[] numbers1, int[] numbers2)
{
int numbers1Sum = Sum(numbers1);
int numbers2Sum = Sum(numbers2);
if (numbers1Sum > numbers2Sum)
Console.WriteLine("the sum of numbers from the numbers1 array is greater");
else if (numbers1Sum < numbers2Sum)
Console.WriteLine("the sum of numbers from the numbers2 array is greater");
else
Console.WriteLine("the sums of numbers in both arrays are equal");
int Sum(int[] numbers)
{
int result = 0;
foreach (int number in numbers)
result += number;
return result;
}
}
int[] numbers1 = { 1, 2, 3 };
int[] numbers2 = { 3, 4, 5, 6, 7 };
Compare(numbers1, numbers2);
Here the sum calculation is moved into the local function Sum, which takes an array and returns its sum. Within the Compare method, we can use it to compute the array sum. It does not matter whether the local function is defined before or after its usage. However, the local function cannot be used outside its method.
Static local functions
Local functions can be static. Such functions are defined using the static keyword. Their feature is that they cannot access the variables of the enclosing method in which the static function is defined.
First, let's define a local function that has access to the environment:
int Sum(int[] numbers)
{
int limit = 0;
int result = 0;
foreach (int number in numbers)
{
if (IsPassed(number)) result += number;
}
return result;
bool IsPassed(int number)
{
return number > limit;
}
}
int[] numbers1 = { -3, -2, -1, 0, 1, 2, 3 };
int[] numbers2 = { 3, -4, 5, -6, 7 };
Console.WriteLine(Sum(numbers1));
Console.WriteLine(Sum(numbers2));
Here the Sum function calculates the sum of numbers in the array that satisfy the condition in the local function IsPassed(). This local function checks whether the passed number is greater than the value of the variable limit defined in the Sum method. That is, the local function IsPassed can access data defined in the enclosing function Sum.
Now, let's make the function IsPassed static:
int Sum(int[] numbers)
{
int result = 0;
int limit = 0;
foreach (int number in numbers)
{
if (IsPassed(number, limit)) result += number;
}
return result;
static bool IsPassed(int number, int lim)
{
//return number > limit; // Error: method IsPassed cannot access variable limit
return number > lim;
}
}
The static modifier is specified before the type of the local function. Now the IsPassed function cannot access the variable limit, and in this case, we need either to pass this value as a parameter or define the variable limit directly inside the local function.