Delegates act as an intermediary between an event source and an event destination. Technically, a delegate is a reference type used to encapsulate a method with a specific signature and return type. You can encapsulate any matching method in that delegate. To be even precise delegates are similar to function pointers. They can be called as type safe function pointers. Unlike function pointers, delegates are object-oriented and type safe. A delegate class used as an intermediary between an event source and event receiver is called an event handler.
Once a delegate is assigned a method, it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value
The signature of a single cast delegate is shown below:
delegate result-type identifier ([parameters]);
· result-type: The result type, which matches the return type of the function.
· identifier: The delegate name.
· parameters: The Parameters, that the function takes.
There are three steps in defining and using delegates:
A delegate is created with the delegate keyword, followed by a return type and the signature of the methods that can be delegated to it, as in the following:
public delegate int MyDelegate(object obj1, object obj2);
This declaration defines a delegate named MyDelegate, which will encapsulate any method that takes two objects as parameters and that returns an int. Once the delegate is defined, you can encapsulate a member method with that delegate by instantiating the delegate, passing in a method that matches the return type and signature. The delegate can then be used to invoke that encapsulated method.
In order to make use of this delegate, you need to instantiate it, to specify the method that needs to be called.
MyDelegate a = new MyDelegate(MyDelegateMethod);
}Here MyDelegateMethod is a method that has a signature similar to that of MyDelegate.
A delegate is used to invoke a method similar to how a method call is made.
For example: MyDelegateMethod(“This is a test invocation”);
Types of Delegates
There are basically two types of delegates. Single Cast delegate and Multi-Cast delegate. A single cast delegate can call only one function. A multi-cast delegate is one that can be part of a linked list. The multi-cast delegate points to the head of such a linked list. This means that when the multi-cast delegate is invoked it can call all the functions that form a part of the linked list. Assume that one has several clients who would like to receive notification when a particular event occurs. Putting all of them in a multi-cast delegate can help call all the clients when a particular event occurs.
To support a single cast delegate the base class library includes a special class type called System.Delegate. To support multi-cast delegates the base class library includes a special class type called SystemMultiCastDelegate.
An event in is a way for a class to provide notifications to clients of that class when some interesting thing happens to an object. The most familiar use for events is in graphical user interfaces; typically, the classes that represent controls in the interface have events that are notified when the user does something to the control (for example, click a button). An event is the outcome of an action. There are two important terms with respect to events. The event source and the event receiver. The object that raises the event is called event source and the object that responds to the event is called event receiver. The communication channel between an event source and an event receiver is the delegate.
Events and Delegates
Declaring an event is directly tied to a delegate. A delegate object encapsulates a method so that it can be called anonymously. An event is a mechanism by which a client class can pass in delegates to methods that need to be invoked whenever “something happens”. When it does, the delegate(s) given to it by its clients are invoked.
public event testDelegate MyEvent;
Once an event is declared, it must be associated with one or more event handlers before it can be raised. An event handler is nothing but a method that is called using a delegate. Use the += operator to associate an event with an instance of a delegate that already exists.
MyForm.MyEvent -= new testEvent(MyMethod);
In C#, events may be raised by just calling them by name similar to method invocation, say MyEvent(10).
How Event works?
Whenever an event is defined for a class, the compiler generates three methods that are used to manage the underlying delegate:
this is a public method that calls the static Combine method of System.Delegate in order to add another method to its internal invocation list. This method is however not used explicitly. The same effect is achieved by using the += operator as specified before.
this is also a public method that calls the static Remove method of System.Delegate in order to remove a receiver from the event’s invocation list. This method is also not called directly. Its job is done by the “-=” operator.
a protected method that calls the compiler generated Invoke method of the delegate, in order to call each method in the invocation list
Courtesy: The Seo Guru, A Software Development Company, Best OOPS Blog Site, Link Submission, Thanks to Shopping Site for Link Exchanging