Repository navigation
EN CSharp Delegates Lambdas
Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어
The delegate stores the called function of a specific signature. Action does not return a result, Func returns one, Predicate<T> returns bool. Lambda is written as arguments => expression or block. The captured variable belongs to the closure, so changing its value affects subsequent calls.
Save this example separately as Example.cs. Run it independently of the other examples on this page.
Example.cs:
// PROJECT-EXAMPLE
System.Func<int, int> twice = value => value * 2;
System.Console.WriteLine(twice(4));Result:
8
Func<int, int, int> add = (left, right) => left + right;
Action<string> report = text => UO.Print(text);
report($"Total={add(2, 3)}");int count = 0;
Action next = () => count++;
next();
next();
UO.Print($"Count={count}");A method can be passed as a method group when its signature matches the delegate. static-lambda does not capture local context. For a task-returning lambda, use Func<Task> or Func<T,Task> and wait for the task; async void is left to event handlers requiring void.
Official Documentation: Delegates, Lambdas, and Closures - Microsoft Learn.
ClassicUO — Bad Newbie & C# Scripts IDE