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 + "<br>");
};
Anodel += delegate(string Address)
{
Response.Write("City he lives is " + Address);
};
Anodel("Chidambaram");
}
The output will be,
Name is Chidambaram
City he lives is Chidambaram
An argumentless anonymous method will be like,
delegate void DelegateTestWithoutParam();
protected void Page_Load(object sender, EventArgs e)
{
//Parameterless anonymous methods
DelegateTestWithoutParam delparam = delegate()
{
Response.Write("<hr><b>Parameterless anonymous
method</b><br><hr>");
Response.Write("Name is Satheesh Babu");
};
delparam();
}
The output will be,
Name is Satheesh Babu
Use of Anonymous methods in Event handling:
Like wise we can use anonymous methods for events like button
click, dropdownlist selected item change, etc. Consider,
btnCheck.Click += new EventHandler(this.btnCheck_Click);
Here “btnCheck.Click” 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)
{
//Event Handling using anonymous Methods
btnCheck.Click += delegate(object s, EventArgs ee)
{
Response.Write("<hr><b>Event Handling using Anonymous
Methods</b><br><hr>");
Response.Write("Name is Satheesh Babu<br><hr>");
};
}
The out put will be,
Event Handling using Anonymous Methods
Name is Satheesh Babu
Note:
1) If a variable is declared outside the anonymous method it
can be accessed inside the anonymous method, i.e. the below code is valid.
delegate void DelegateTestWithoutParam();
protected void Page_Load(object sender, EventArgs e)
{
string name = “Satheesh Babu”;
//Parameterless anonymous methods
DelegateTestWithoutParam delparam = delegate()
{
Response.Write("<hr><b>Parameterless anonymous
method</b><br><hr>");
Response.Write("Name is "+ name);
};
delparam();
}
The below code which is trying to access a variable declared
inside anonymous method is invalid and will throw a compilation error.
delegate void DelegateTestWithoutParam();
protected void Page_Load(object sender, EventArgs e)
{
//Parameterless anonymous methods
DelegateTestWithoutParam delparam = delegate()
{
string name = “Satheesh Babu”;
Response.Write("<hr><b>Parameterless anonymous
method</b><br><hr>");
};
Response.Write("Name is "+ name);
delparam();
}
2) 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("<hr><b>Parameterless anonymous method assigned
to a delegate with different signature</b><br><hr>");
Response.Write("Name is Satheesh Babu");
};
DiffAnodel("Satheesh");
}
The output will be,
Parameterless anonymous method assigned to a delegate with
different signature
Name is Satheesh Babu
Thus, we can define an anonymous method as a
method without name which allows us to define the body inline.
|