Showing posts with label Overview of Delegates. Show all posts
Showing posts with label Overview of Delegates. Show all posts

Monday, April 7, 2008

Anonymous delegate using Dotnet framework 2.0

System.Threading.Thread Th = new System.Threading.Thread(delegate()
{
System.Console.Write("hi");
});
Th.Start();

Tuesday, September 25, 2007

Ananoymous Delegate

Ananymous Delegate:

protected void Page_Load(object sender, EventArgs e)
{
btn1.Click += delegate
{
Response.Write("Bala");
};

}

Note:If you use ananoymous delegate no need of creating seprate method for button click.This is Dotnet framework 2.0 :

Out put :

Bala

Note:When you click on button you will get above msg(Bala)

Multicast Delegate

Multicast Delegate:

delegate void getvalue();

public partial class Global : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
getvalue test=new getvalue(testbala);
//Note:Single instance we have to call two method that is called multicast delegate
test += new getvalue(testbala1);
test();
}

protected void testbala()
{
Response.Write("Bala");
}
protected void testbala1()
{
Response.Write("Bala1");
}

Output:

Bala
Bala1

Overview of Delegates(C#)


Definetion:Delegate is nothing but the pointing the function.

How to call method using Delegate:

delegate void getvalue(); //Above class you have to declare delegate
public partial class Global : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

getvalue test=new getvalue(testbala);
test();
}
protected void testbala()
{
Response.Write("Bala");
}

Output

Bala