Library tutorials & articles
Event Handling in .NET Using C#
- Introduction
- Delegates in C#
- Event Handlers in C#
- GUI Event Handling
- Conclusion
Delegates in C#
A delegate in C# allows you to pass methods of one class to objects of other classes that can call those methods. You can pass method m in Class A, wrapped in a delegate, to class B and Class B will be able to call method m in class A. You can pass both static and instance methods. This concept is familiar to C++ developers who have used function pointers to pass functions as parameters to other methods in the same class or in another class. The concept of delegate was introduced in Visulal J++ and then carried over to C#. C# delegates are implemented in .Net framework as a class derived from System.Delegate. Use of delegate involves four steps.
1. Declare a delegate object with a signature that exactly matches the method
signature that you are trying to encapsulate.
2. Define all the methods whose signatures match the signature of the delegate
object that you have defined in step 1.
3. Create delegate object and plug in the methods that you want to encapsulate.
4. Call the encapsulated methods through the delegate object.
The following C# code shows the above four steps implemented using one delegate and four classes. Your implementation will vary depending on the design of your classes.
using System;
//Step 1. Declare a delegate with the signature of the encapsulated method
public delegate void MyDelegate(string input);
//Step 2. Define methods that match with the signature of delegate declaration
class MyClass1{
public void delegateMethod1(string input){
Console.WriteLine("This is delegateMethod1 and the
input to the method is {0}",input);
}
public void delegateMethod2(string input){
Console.WriteLine("This is delegateMethod2 and the
input to the method is {0}",input);
}
}
//Step 3. Create delegate object and plug in the methods
class MyClass2{
public MyDelegate createDelegate(){
MyClass1 c2=new MyClass1();
MyDelegate d1 = new MyDelegate(c2.delegateMethod1);
MyDelegate d2 = new MyDelegate(c2.delegateMethod2);
MyDelegate d3 = d1 + d2;
return d3;
}
}
//Step 4. Call the encapsulated methods through the delegate
class MyClass3{
public void callDelegate(MyDelegate d,string input){
d(input);
}
}
class Driver{
static void Main(string[] args){
MyClass2 c2 = new MyClass2();
MyDelegate d = c2.createDelegate();
MyClass3 c3 = new MyClass3();
c3.callDelegate(d,"Calling the delegate");
}
}
Related articles
Related podcasts
-
Object-Oriented Programming in Ruby
In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...
Events coming up
-
Dec
7
An Event Apart San Francisco 2009
San Francisco, United States
From the makers of A List Apart: An Event Apart is an intensely educational two-day conference for passionate practitioners of standards-based web design. If you care about code as well as content, usability as well as design, An Event Apart is th...
While it's very nice to know the "How" when explaining syntax, more importantly is they "why" in terms of practical real-life use within a C# application. It would be nice of you would have included some true examples of when you would need to use one and why.
This is one of the best articles i have come across on Delegates and Event.. Good Job!
Line:
public class MyEventArgs EventArgs {
should read:
public class MyEventArgs : EventArgs {
Otherwise, great article!
Can anyone help me pls!!!
I am facing a big problem to code the gui buttons!! I am doing a C# project. this project consists of a telephone graphical user interface!!
can anyone tell me what can i do to code the number buttons to display the telephone number into a textfield...
I need this urgent plsssss!!!!!!
Thanks for your help!!!!!!
I need to know how can I trap an Outlook Event (Saving an Item) using C#. Any example will be highly appreciated.
Thanks in advance!!
How can I create asynchrous events?
Using delegates and events I get synchronous calls?
Please advise.
I am using certain, COM Components, I need to know the delegate signature, to create event handler. Is there any tools to do this? or if I need to do manually which is the easiast way to know the exact signature of those events raised by the COM components.
Very good example. it can be better hadling browser events, with .NET of course.
This thread is for discussions of Event Handling in .NET Using C#.