Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Functions and parameters: partial application and closures

Understand methods, local functions, and lambdas in C#. Learn how parameters differ from arguments, how to create a function with one argument fixed, and why a closure observes changes to a captured variable.

Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification

Syntax

static int Add(int left, int right) => left + right;
Func<int, int> addTen = value => Add(10, value);

In C#, behavior can be expressed as a method, a local function, or an anonymous function such as a lambda. A method belongs to a type. A local function can be declared in a member body or among top-level statements, where no enclosing member declaration is needed in the source. The form value => expression is an example of a lambda with one parameter and an expression body. A lambda can have zero, one, or multiple parameters, and its body can be an expression or a block of statements. A parameter is a named variable in a declaration; an argument is a value or expression supplied in a call. Arguments are passed by value by default; for a reference type, the reference is copied, not the object.

A delegate lets you store and pass callable behavior. Func<int, int> describes a delegate that accepts one int and returns an int; Action<int> returns no value. A lambda can be converted to a compatible delegate type. Local functions suit named helper operations, while lambdas suit short behavior passed as an argument.

Partial application fixes some arguments and creates a function that accepts the remaining ones. C# does not do this automatically when you omit a required argument: you can explicitly create a lambda that wraps the call. In the example, BindLeft(10) returns a delegate that needs only right. This is different from an optional parameter, which supplies a default value at a call site.

A closure combines a function with captured variables from its surrounding scope. It captures the variable, not a snapshot of its value: after offset = 20, calling addOffset(3) produces 23. A captured variable can outlive the call that created it, as left does in BindLeft. Watch for shared mutable state and objects being kept alive longer. A lambda or local function marked static cannot capture local variables or instance state.

Reference identifiers covered by this entry: Partial application and closures.

Examples

using System;

internal static class Program
{
    private static int Add(int left, int right) => left + right;

    private static Func<int, int> BindLeft(int left) =>
        right => Add(left, right);

    private static void Main()
    {
        Func<int, int> addTen = BindLeft(10);
        Console.WriteLine(addTen(3));

        int offset = 10;
        Func<int, int> addOffset = value => Add(offset, value);
        offset = 20;
        Console.WriteLine(addOffset(3));
    }
}

Result

13
23

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources