Level 6 — Generics and Delegates
Anonymous functions
Original notes by SunDiaL · View English source
Functions are normally named so they can be reused and referenced. But what if you just want a quick, one-off function to use as a delegate? That's what anonymous functions are for. An anonymous function is simply a function definition without a name.
delegate int SomeDelegateType(int x);
public void Main()
{
SomeDelegateType d = delegate(int x) { return x + 1; };
}
Note: You must explicitly declare parameter types.
Anonymous functions are the predecessor to lambdas. Today, you should prefer lambdas unless you're working with legacy code.