Monday, April 28, 2008

Anonymous Methods

To write an anonymous method, include the delegate keyword followed by a parameter declarationa dn then a method body:

First create a delegate Transformer:

delegate int Transformer (int i);

  • Transformer square = delegate(int x){return x * x;};
  • this statement can be simplied to :
  • Transformer square = (int x ) => {return x * x;}
  • OR
  • Transformer squere = x => x* x;

call square:

Console.WriteLine(square(5));

No comments: