Skip to content

Lambdas & closures

Inline lambdas inside a method you are patching usually just work - the whole method is recompiled when you save.


Lambdas inside methods

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

Change the lambda, save the file, and the next Update uses the new version.


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 earlier (Awake / Start), editing the lambda updates the method body, but the static field may still hold the old delegate.

What to do: Re-assign the field (re-run that setup code), or restart Play Mode.


Closures

Lambdas that capture variables that already existed keep working after a patch.

If you add a new captured variable, the closure updates only when that lambda is created again at runtime. Lambdas built each call (for example inside a sort comparer) pick up changes immediately.