0% found this document useful (0 votes)
99 views6 pages

What Is A Delegate ?

A delegate is a type that holds a reference to a method. It allows methods to be passed around as arguments or returned as values from other methods. Delegates provide a way to decouple classes and allow for asynchronous method calls. Multicast delegates can reference multiple methods and are useful for events. Events use delegates to allow classes to notify other classes of actions without knowing who the receiver is.

Uploaded by

corust
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views6 pages

What Is A Delegate ?

A delegate is a type that holds a reference to a method. It allows methods to be passed around as arguments or returned as values from other methods. Delegates provide a way to decouple classes and allow for asynchronous method calls. Multicast delegates can reference multiple methods and are useful for events. Events use delegates to allow classes to notify other classes of actions without knowing who the receiver is.

Uploaded by

corust
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 6

What is a Delegate ?

Delegate is a type which holds the method(s) reference in an object. It is also referred to as a type safe function pointer.

Advantages
Encapsulating the method's call from caller Effective use of delegate improves the performance of application Used to call a method asynchronously

Why Delegates?

In short the problem is that there is a tight coupling of function names with the UI client. So how can we solve this problem?. Rather than referring to the actual methods in the UI / client if we can refer an abstract pointer which in turn refers to the methods then we can decouple the functions from UI. Later any change in the class ClsMath will not affect the UI as the changes will be decoupled by the Abstract pointer. This abstract pointer can be defined by using delegates. Delegates define a simple abstract pointer to the function / method.

Declaration
public delegate int mydelegate(int delvar1,int delvar2)

Note
You can use delegates without parameters or with parameter list You should follow the same syntax as in the method (If you are referring to the method with two int parameters and int return type, the delegate which you are declaring should be in the same format. This is why it is referred to as type safe function pointer.)

Sample Program using Delegate


public delegate double Delegate_Prod(int a,int b); class Class1 { static double fn_Prodvalues(int val1,int val2) { return val1*val2; } static void Main(string[] args)

{ //Creating the Delegate Instance Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues); Console.Write("Please Enter Values"); int v1 = Int32.Parse(Console.ReadLine()); int v2 = Int32.Parse(Console.ReadLine()); //use a delegate for processing double res = delObj(v1,v2); Console.WriteLine ("Result :"+res); Console.ReadLine(); } } Explanation Here I have used a small program which demonstrates the use of delegate. The delegate "Delegate_Prod" is declared with double return type and accepts only two integer parameters. Inside the class, the method named fn_Prodvalues is defined with double return type and two integer parameters. (The delegate and method have the same signature and parameter type.) Inside the Main method, the delegate instance is created and the function name is passed to the delegate instance as follows: Delegate_Prod delObj = new Delegate_Prod(fn_Prodvalues); After this, we are accepting the two values from the user and passing those values to the delegate as we do using method: delObj(v1,v2); Here delegate object encapsulates the method functionalities and returns the result as we specified in the method.

Multicast Delegate
What is Multicast Delegate?
It is a delegate which holds the reference of more than one method. Multicast delegates must contain only methods that return void, else there is a run-time exception.

Simple Program using Multicast Delegate


delegate void Delegate_Multicast(int x, int y); Class Class2 { static void Method1(int x, int y) { Console.WriteLine("You r in Method 1"); } static void Method2(int x, int y) { Console.WriteLine("You r in Method 2"); } public static void Main() { Delegate_Multicast func = new Delegate_Multicast(Method1); func += new Delegate_Multicast(Method2); func(1,2); func(2,3); } } // Method1 and Method2 are called // Only Method2 is called func -= new Delegate_Multicast(Method1);

Explanation
In the above example, you can see that two methods are defined named method1 and method2 which

take two integer parameters and return type as void. In the main method, the Delegate object is created using the following statement: Delegate_Multicast func = new Delegate_Multicast(Method1);Then the Delegate is added using the += operator and removed using the -= operator.

Events and Delegates


An event is a message sent by an object to signal the occurrence of an action. The action could be caused by user interaction, such as a mouse click, or it could be triggered by some other program logic. The object that raises the event is called the event sender. The object that captures the event and responds to it is called the event receiver. In event communication, the event sender class does not know which object or method will receive (handle) the events it raises. What is needed is an intermediary (or pointer-like mechanism) between the source and the receiver. The .NET Framework defines a special type (Delegate) that provides the functionality of a function pointer. A delegate is a class that can hold a reference to a method. Unlike other classes, a delegate class has a signature, and it can hold references only to methods that match its signature.While delegates have other uses, the discussion here focuses on the event handling functionality of delegates. A delegate declaration is sufficient to define a delegate class. The declaration supplies the signature of the delegate. public delegate void AlarmEventHandler(object sender, AlarmEventArgs e); The syntax is similar to that of a method declaration; however, the delegate keyword informs the compiler that AlarmEventHandler is a delegate type. By convention, event delegates in the .NET Framework have two parameters, the source that raised the event and the data for the event. An instance of the AlarmEventHandler delegate can bind to any method that matches its signature, such as the AlarmRang method of the WakeMeUp class shown in the following example. public class WakeMeUp { // AlarmRang has the same signature as AlarmEventHandler. public void AlarmRang(object sender, AlarmEventArgs e) { //...

} //... } References : http://www.codeproject.com/KB/cs/Delegates_in_C_.aspx http://www.codeproject.com/KB/cs/6ImportDelegatesAndEvents.aspx http://msdn.microsoft.com/en-us/library/17sde2xt.aspx http://msdn.microsoft.com/en-us/vjsharp/bb188664.aspx

You might also like