Anonymous Method:
It is a method without any name.i.e. A normal method is one which will have a name, a return type, optionally arguments and an access modifier. So an anonymous method is a feature to have methods without name.
Where should i use an Anonymous method??
Anonymous method could be used in place where there is a use of delegate.
Delegate:
A delegate is similar to function pointer in C++ and C. A function pointer in C is a variable that points to a function. Similarly a delegate is a reference type in .net that is used to call the methods of similar signature as of the delegate.
Ex.
void PrintName(string name)
{
Response.Write("Name is " + name + "<\br>");
}
To call the above method using delegate, we should declare a delegate with similar signature.
Ex.
delegate void DelegateTest(string n);
Before calling we should initialize or register the delegate with the method.
Ex.
DelegateTest del = new DelegateTest (this.PrintName);
Finally, can call the function by,
Del("Malathy");
The difference between the function pointer in C language and a delegate is, at a given time a function pointer can point to a single funtion only. But delegate are by default they are multicast delegate. ie. they can be used to call multiple functions at a time.
Ex. Consider the function,
void PrintAddress(string address)
{
Response.Write("City he lives is " + address);
}
To call both PrintName and PrintAddress using delegate, declare as follows.
DelegateTest del = new DelegateTest (this.PrintName);
del += new DelegateTest (this.PrintAddress);
del("Chidambaram");
Now the output is,
Name is Chidambaram.
City he lives is Chidambaram.
By the use of += operator we add new methods to delegates. Thus += operator is used to register a function and -= will remove the registration from the delegate.
Delegate del = new DelegateTest(this.PrintName);
del += new DelegateTest(this.PrintAddress);
del -= new DelegateTest(this.PrintAddress);
del("Chidambaram");
Now the output is
Name is Chidambaram.
The other difference between a delegate and a function pointer is, a delegate is a type safe, ie.it will throw a compilation error if a method with different signature is assigned to the delegate.
Other than the above examples, the delegates are used in event handling, for callbacks ets. For example, in Webform designer code in VS, there will be,
btnCheck.Click += new EventHandler(this.btnCheck_Click);
Here the event handler is a delegate that is registered with btnCheck_Click event.
Anonymous Methods
The syntax consists of the keyword delegate, an optional parameter list and method body enclosed in parenthesis.
Ex.
delegate(Optional parameters)
{
Body of the method.
}
Where to use Anonymous Methods??
The anonymous representation to implement the PrintName method will be,
delegate void DelegateTest(string n);
protected void Page_Load(object sender, EventArgs e)
{
DelegateTest PrintName = delegate (String Name)
{
Response.Write("Name is " + Name );
};
}
Can be called by
PrintName("Malathy");
And the outpt is
Name is Malathy.
Similar to delegate, we can use += operator to call multiple methods.
Ex.
delegate void DelegateTest (string n);
protected void Page_Load(Object sender, EventArgs e)
{
DelegateTest test = delegate (StringName)
{
Response.Write("Name is " + Name );
};
test += delegate (String Address)
{
Response.Write("City he lives is " + Address);
};
test("Chidambaram");
}
The output is
Name is Chidambaram
City he lives is Chidambaram
An anonymous method without any argument will be like,
delegate void DelegateTestWithoutParam();
protected void Page_Load (object sender, EventArgs e)
{
DelegateTestWithoutParam delparam = delegate()
{
Response.Write("Parameterless anonymous method");
Response.Write("Name is Malathy");
};
delparam();
}
Here the output will be,
Name is Malathy.
Use of Anonymous Methods in Event Handling:
We can use anonymous methods for events like button click, .
Ex.
btnCheck.Click += new EventHandler(this.btnCheck_Click);
Here btnClick is expecting a delegate and hence we can use anonymous methods. So the event can be written as,
protected void Page_Load(object sender, EventArgs e)
{
btnClick.Click += delegate(object s, EventArgs ee)
{
Response.Write ("Name is Malathy");
};
}
Here the output on clicking the button is
Name is Malathy.
Important Notes:
1. If a variable is declared outside the anonymous method then, it can be accessed inside the anonymous method.
2. Variables declared inside the anonymous methods are not accessible outside the anonymous method.
3. When a anonymous mehtod is declares without parenthesis, then it can be assigned to a delegate with any signature.
In short:
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 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.
Hai Malathy Garu............I have read min 20 websites for Delegates and Anonymous Methods.But your way of explenation is very good.Because new comers also easily understands.Thank You.....
ReplyDelete