Skip to content

Lambdas & Closures

Simple lambdas

Editing a lambda inside a patched method works because the entire method body is recompiled.

void Update() {
    var active = GetItems().Where(x => x.IsActive); // edit and save
}

Lambdas stored in static fields

static Func<int, int> s_transform = x => x * 2;

void Update() {
    Debug.Log(s_transform(5));
}

If s_transform was assigned during Awake or Start, editing the lambda updates the method but the static field may still hold the old delegate until it is re-assigned. Re-run the assignment code or restart Play Mode.


Closures

Lambdas that capture existing local variables or fields continue to work after a patch.

If you add a new captured variable, the closure only updates when the lambda is re-created at runtime. For example, a lambda inside Array.Sort(() => ...) is re-created each call and will pick up changes immediately.