0Pricing
C# Academy · Lesson

Declaring Local Functions

Define helper functions inside methods.

What Is a Local Function?

A local function is a method declared inside another method. It is only visible within its enclosing member, which keeps helper logic close to where it is used and out of the class surface.

Declaring One

Write the local function like a normal method, nested inside the body. You can call it before or after its declaration within the same scope.

using System;

public class Program
{
    public static void Main()
    {
        int Square(int n) => n * n;

        Console.WriteLine(Square(4));
        Console.WriteLine(Square(7));
    }
}

All lessons in this course

  1. Declaring Local Functions
  2. Closures and Captured Variables
  3. Static Local Functions
  4. Local Functions vs Lambdas
← Back to C# Academy