Local functions are functions defined inside another method. They help organize code by keeping helper functions close to where they are used, improving readability and encapsulation.
Main
or any other method).
class Program
{
static void Main()
{
Console.WriteLine("Factorial of 5 is: " + Factorial(5));
// Local function to calculate factorial
int Factorial(int n)
{
if (n <= 1)
return 1;
return n * Factorial(n - 1);
}
}
}
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!