An anonymous method is like a lambda expression - CSharp Custom Type

CSharp examples for Custom Type:Lambda

Introduction

To write an anonymous method, you include the delegate keyword followed (optionally) by a parameter declaration and then a method body.

delegate int Transformer (int i);

Transformer sqr = delegate (int x) {return x * x;};
Console.WriteLine (sqr(3));                            // 9

It is semantically equivalent to the following lambda expression:

Transformer sqr =       (int x) => {return x * x;};

or simply:

Transformer sqr =            x  => x * x;

Anonymous methods capture outer variables in the same way lambda expressions do.


Related Tutorials