Skip to content

Implicit capture fails with nullable value and @ symbol #2573

Open

Description

@Grimelios
private static void MyFunction<T>(T callback) where T : delegate void()
{
}

public static void Main()
{
    let nullableInt = (int?)123;

    if (let int1 = nullableInt)
    {
        // This call compiles successfully.
        MyFunction(() => Console.WriteLine(int1));
    }

    if (let @int2 = nullableInt)
    {
        // Error: "Failed to lookup implicit capture `int2`"
        MyFunction(() => Console.WriteLine(@int2));
    }
    
    let @int3 = 123;

    // This call also compiles successfully.
    MyFunction(() => Console.WriteLine(@int3));
}

Tested on 10/15/2026. As shown, implicit capture fails in the second example, where the nullable integer is cast to a plain integer (within the if statement), but only when the variable is prefixed with @. The third example demonstrates that not all @ variables fail to capture.

If you're wondering why I'd bother using @ in the first place, I discovered this issue when using a nullable enum value and attempting a similar setup with a variable named @enum. I first assumed that the issue was because enum is a reserved keyword, but the code above demonstrates that that assumption was wrong.

let e = MyEnum.ABC;

if (let @enum = e)
{
    // Error: "Failed to lookup implicit capture 'enum'"
    MyFunction(() => Console.WriteLine(@enum));
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions