how to use the event in the c# and delegate

Posted by Rajamani under C# on 12/3/2012 | Points: 10 | Views : 1483 | Status : [Member] | Replies : 4
how to create the delegate?
how to create the event?




Responses

Posted by: Sheonarayan on: 12/3/2012 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hi Rajamani,

There are couple of articles available on Delegates and events on this website, the easiest way to learn is to read and practice them http://www.dotnetfunda.com/search/search.aspx?q=delegates

This is http://www.dotnetfunda.com/articles/article1670-what-are-delegates-in-csharp.aspx good to start with.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Rajamani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vforvijay on: 12/3/2012 [Member] Starter | Points: 25

Up
0
Down
Hi..
Refer the below link to use delegate in C#
http://www.dotnetcode.in/2012/10/delegates-and-multicast-delegates-with.html

Rajamani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 12/5/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Refer this url
http://www.codeproject.com/Articles/4773/Events-and-Delegates-Simplified
Delegates

delegate void Procedure();
class Program
{
public static void Method1()
{
Console.WriteLine("Method 1");
}
public static void Method2()
{
Console.WriteLine("Method 2");
}
public void Method3()
{
Console.WriteLine("Method 3");
}
static void Main()
{
Procedure ObjDelegate = null;
ObjDelegate += new Procedure(Program.Method1); // Adding method reference.
ObjDelegate += new Procedure(Program.Method2);
Program demo = new Program(); // Class Object
ObjDelegate += new Procedure(demo.Method3);
// ObjDelegate -= new Procedure(Program.Method1); // Removing method reference.
ObjDelegate();
Console.ReadKey();
}
}

Anonymous Delegates

delegate void Procedure();
class Program
{
static Procedure ObjDelegate = null;
private static void AddProc()
{
int variable = 100;
ObjDelegate += new Procedure(delegate
{
Console.WriteLine(variable);
}
);
}
static void Main()
{
ObjDelegate += new Procedure(delegate
{
Console.WriteLine("test");
}
);
AddProc();
ObjDelegate();
Console.ReadKey();
}
}

Anonymous Delegate with Arguments
 

delegate void Procedure(string text);
class Program
{
static Procedure ObjDelegate = null;
private static void AddProc()
{
int variable = 100;
ObjDelegate += new Procedure(delegate(string text)
{
Console.WriteLine(text + ", " + variable.ToString());
});
}
static void Main()
{
ObjDelegate += new Procedure(delegate(string text) { Console.WriteLine(text); });
AddProc();
ObjDelegate("Testing");
Console.ReadKey();
}
}


Rajamani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 12/6/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
Hi @Rajamani
plz Mark As Answer if post will helps you.

Rajamani, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response