Wednesday, August 3, 2016

Anonymous method in c#

Anonymous method


When to use anonymous methods

  • If we want to use the delegate only inside the same function
  • Reduce overhead of instantiating delegate i.e. performance improvement
E.g.

class program
{
delegate int pointToAddFunction(int n1,int n2);

static void Main (string[] args)
{
//Anonymous method is below
pointToAddFunction ptrObj = delegate
(int n1,int n2)
{
return n1 +n2;
};     //Note semicolon
//Anonymous method ended

int x = ptrObje.Invoice(2,2);
console.writeline(x);
}
}
 

No comments:

Post a Comment