Thursday, May 12, 2011

Anonymous Methods In C#

An anonymous method is a method without any name. To understand better, a normal method is one which will have a name, return type optionally arguments and an access modifier.
Anonymous methods can be used in the place where there is a use of a delegate. To understand anonymous methods we should understand the usage of delegate first. The following section of this article gives you an idea about delegate.

Understanding Delegate:

Delegate is similar to function pointer in C and C++. A function pointer in C is a variable that points to function. Similarly, a delegate is a reference type in .net that is used to call the methods of similar signature as of the delegate. For example, consider a C# method,

void PrintName(string Name)

{

Response.Write("Name is "+Name+"
");

}

To call the above method using delegate we should declare a delegate with similar signature,

delegate void DelegateTest(string n);

Before calling we should initialize or register the delegate with the method like,

DelegateTest del = new DelegateTest(this.PrintName);

Finally we can call the function by,

Del(“Pankaj”);

So what makes the real difference between C language function pointer and a delegate is, at a given time a function pointer can point to a single function only i.e. it can be used to call a single function only. But delegate are by default they are multicast delegate, means they can be used to call multiple functions at a time. Consider a function,

void PrintAddress(string Address)

{

Response.Write("City he lives is "+Address);

}

The below code will explain how to call PrintName and PrintAddress methods using a delegate,

DelegateTest del = new DelegateTest(this.PrintName);

del += new DelegateTest(this.PrintAddress);

del(“Pankaj”);


So the output will be,


Name is Pankaj

City he lives is Pankaj


It normally does it use of the += operator. So += operator is used to register a function and -= will remove the registration from the delegate. Consider,


DelegateTest del = new DelegateTest(this.PrintName);

del += new DelegateTest(this.PrintAddress);

del -= new DelegateTest(this.PrintAddress);

The output of above code will be,


Name is Pankaj


The other difference between a delegate and a function pointer is, a delegate is type safe, means it will throw a compilation error if a method with different signature is assigned to the delegate.


Back to Anonymous methods:

The syntax of an anonymous method consists of the keyword delegate, an optional parameter list and method body enclosed in parenthesis.


delegate(Optional parameters)

{

Body of the method.

};


So looking at the answer of the question “Where should I use an Anonymous method?” the anonymous representation to implement the PrintName method will be,


delegate void DelegateTest(string n);

protected void Page_Load(object sender, EventArgs e)

{

DelegateTest Anodel = delegate(string Name)

{

Response.Write("Name is " + Name + "
");

};

}


Calling is again by,


Anodel(“Pankaj”);


The output will be,


Name is Pankaj


Similar to delegate, To call multiple methods we can use += operator,


delegate void DelegateTest(string n);

protected void Page_Load(object sender, EventArgs e)

{

DelegateTest Anodel = delegate(string Name)

{

Response.Write("Name is " + Name + "
");

};

Anodel += delegate(string Address)

{

Response.Write("City he lives is " + Address);

};

Anodel("Pankaj");

}

The output will be,

Name is Pankaj

City he lives is Pankaj



IMPORTANT NOTE : When an anonymous method is declared without parenthesis, it can be assigned to a delegate with any signature.

delegate void DelegateTest(string n);

protected void Page_Load(object sender, EventArgs e)

{

DelegateTest DiffAnodel = delegate

{

Response.Write("


Parameterless anonymous method assigned to a delegate with different signature

");

Response.Write("Name is Pankaj");

};

DiffAnodel("Pankaj3");

}

The output will be,

Parameterless anonymous method assigned to a delegate with different signature

Name is Pankaj

Thus,

"
we can define an anonymous method as a method without name which allows us to define the body inline."


Why should we use Anonymous method?

We can reduce the code by preventing delegate instantiation and registering it with methods..

It increases the readability and maintainability of our code by keeping the caller of the method and the method itself as close to one another as possible

No comments:

Post a Comment