Local functions
Local functions can be achievable using function and action using anonymous methods in prior versions, but there are still a few limitations:
- Generics
ref
andout
parametersparams
Local functions are featured to declare within the block scope. These functions are very powerful and have the same capability as any other normal function but with the difference that they are scoped within the block these were declared.
Consider the following code-example:
public static string FindOddEvenBySingleNumber(int number) => IsOddNumber(number) ? "Odd" : "Even";
The method FindOddEvenBySingleNumber()
in the preceding code is simply returning a number as Odd or Even for numbers greater than 1. This uses a private method, IsOddNumber()
, as shown here:
private static bool IsOddNumber(int number) => number >= 1 && number % 2 != 0;
The method IsOddNumber()
is a private method and is available within the class it declared. Hence, its scope is within a class and not within a code block...