0Pricing
C# Academy · Lesson

Declaring Local Functions

Define helper functions inside methods.

Declaring Local Functions is a free C# Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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));
    }
}

Block-Bodied Local Functions

Local functions can have full block bodies with multiple statements, loops, and their own local variables, just like any method.

using System;

public class Program
{
    public static void Main()
    {
        int Factorial(int n)
        {
            int result = 1;
            for (int i = 2; i <= n; i++) result *= i;
            return result;
        }

        Console.WriteLine(Factorial(5));
    }
}

Improving Readability

Local functions let you name a piece of logic without polluting the class with a private method that only one method uses. This keeps related code together.

using System;

public class Program
{
    public static void Main()
    {
        string Format(string label, int value) => label + ": " + value;

        Console.WriteLine(Format("Score", 90));
        Console.WriteLine(Format("Level", 3));
    }
}

Recursion With Local Functions

A local function can call itself, making recursion easy to express right where it is needed.

using System;

public class Program
{
    public static void Main()
    {
        int Fib(int n) => n < 2 ? n : Fib(n - 1) + Fib(n - 2);

        for (int i = 0; i < 7; i++)
            Console.Write(Fib(i) + " ");
        Console.WriteLine();
    }
}

Local Functions Can Be Declared After Use

Unlike local variables, a local function may be referenced earlier in the method than where it is declared. The compiler resolves the whole enclosing scope.

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine(Greet("Ada")); // used before declared

        string Greet(string name) => "Hello, " + name;
    }
}

Parameters and Return Types

Local functions support everything regular methods do: multiple parameters, return values, ref and out parameters, and generics.

using System;

public class Program
{
    public static void Main()
    {
        bool TryHalve(int n, out int half)
        {
            half = n / 2;
            return n % 2 == 0;
        }

        if (TryHalve(10, out int result))
            Console.WriteLine("half is " + result);
    }
}

Generic Local Functions

A local function can be generic, parameterized over its own type arguments independent of the enclosing method.

using System;

public class Program
{
    public static void Main()
    {
        T Echo<T>(T value)
        {
            Console.WriteLine("echoing " + value);
            return value;
        }

        int n = Echo(42);
        string s = Echo("hi");
        Console.WriteLine(n + " " + s);
    }
}

Nesting Local Functions

Local functions can themselves contain local functions. Inner ones are visible only within their parent, allowing tightly scoped helper layers.

using System;

public class Program
{
    public static void Main()
    {
        int Compute(int x)
        {
            int Double(int n) => n * 2;
            int AddOne(int n) => n + 1;
            return Double(AddOne(x));
        }

        Console.WriteLine(Compute(5));
    }
}

Local Functions for Argument Validation

A common pattern: validate arguments eagerly, then defer the real work to a local function. This is especially useful with iterators so validation runs immediately.

using System;
using System.Collections.Generic;

public class Program
{
    static IEnumerable<int> Range(int start, int count)
    {
        if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
        return Iterate();

        IEnumerable<int> Iterate()
        {
            for (int i = 0; i < count; i++) yield return start + i;
        }
    }

    public static void Main()
    {
        foreach (var n in Range(10, 3)) Console.WriteLine(n);
    }
}

When to Use Local Functions

Reach for a local function when a helper is used by exactly one method, benefits from a descriptive name, or needs access to the method local state. It keeps the class clean and the logic discoverable.

using System;

public class Program
{
    public static void Main()
    {
        int total = 0;
        void Add(int n) => total += n; // helper tied to this method
        Add(3); Add(4); Add(5);
        Console.WriteLine(total);
    }
}

Quick Check

Recall what a local function is.

Recap

Local functions are named helpers nested inside a method.

  • Visible only within their enclosing member.
  • Support parameters, returns, out/ref, generics, and recursion.
  • Can be referenced before their declaration.
  • Great for single-use helpers and eager argument validation in iterators.

Frequently asked questions

Is the “Declaring Local Functions” lesson free?

Yes — the full text of “Declaring Local Functions” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Declaring Local Functions”?

Define helper functions inside methods. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Declaring Local Functions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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