Thursday, June 18, 2009

Delegates and Events in C#.Net

Delegates in C# .Net:

If we look at C++ there is a feature called callback function. This feature uses Pointers to Functions to pass them as parameters to other functions. Delegate is a similar feature but it is more type safe, which stands as a stark contrast with C++ function pointers. A delegate can hold reference/s to one more more functions and invoke them as and when needed.

A delegate needs the method's name and its parameters (input and output variables) when we create a delegate. But delegate is not a standalone construction. it's a class. Any delegate is inherited from base delegate class of .NET class library when it is declared. This can be from either of the two classes from System.Delegate or System.MulticastDelegate.

If the delegate contains a return type of void, then it is automatically aliased to the type of System.MulticastDelegate. This can support multiple functions with a += operator. If the delegate contains a non-void return type then it is aliased to System.Delegate class and it cannot support multiple methods.

A delegate is a class that encapsulates a method signature. Although it can be used in any context, it often serves as the basis for the event-handling model in C# but can be used in a context removed from event handling (e.g. passing a method to a method through a delegate parameter).


public delegate int DelegateMethod(int x, int y);

Any method that matches the delegate's signature, which consists of the return type and parameters, can be assigned to the delegate. This makes is possible to programmatically change method calls, and also plug new code into existing classes. As long as you know the delegate's signature, you can assign your own-delegated method.

This ability to refer to a method as a parameter makes delegates ideal for defining callback methods.

Delegate magic :

In class we create its object, which is instance, but in delegate when we create instance that is also referred as delegate (means whatever you do you will get delegate).

Delegate does not know or care about the class of the object that it references. Any object will do; all that matters is that the method's argument types and return type match the delegate's. This makes delegates perfectly suited for "anonymous" invocation.

Benefit of delegates:
In simple words delegates are object oriented and type-safe and very secure as they ensure that the signature of the method being called is correct. Delegate helps in code optimization.

Types of delegates:

1) Singlecast delegates

2) Multiplecast delegates

Delegate is a class. Any delegate is inherited from base delegate class of .NET class library when it is declared. This can be from either of the two classes from System.Delegate or System.MulticastDelegate.

Singlecast delegate
Singlecast delegate point to single method at a time. In this the delegate is assigned to a single method at a time. They are derived from System.Delegate class.

Multicast Delegate

When a delegate is wrapped with more than one method that is known as a multicast delegate.

In C#, delegates are multicast, which means that they can point to more than one function at a time. They are derived from System.MulticastDelegate class.

Events in C# .Net:

Delegate usefulness does not just lie in the fact that it can hold the references to functions but in the fact that it can define and use function names at runtime and not at compile time. A large goal of design delegates is their applicability in events model of .Net. Events are the actions of the system on user manipulations (e.g. mouse clicks, key press, timer etc.) or any event triggered by the program .

Event and delegate are linked together. Event is a reference of delegate i.e. when event will be raised delegate will be called. In C# terms, events are a special form of delegate. Events are nothing but change of state. Events play an important part in GUI programming. Events and delegates work hand-in-hand to provide a program's functionality. A C# event is a class member that is activated whenever the event it was designed for occurs.

It starts with a class that declares an event. Any class, including the same class that the event is declared in, may register one of its methods for the event. This occurs through a delegate, which specifies the signature of the method that is registered for the event. The event keyword is a delegate modifier. It must always be used in connection with a delegate.

The delegate may be one of the pre-defined .NET delegates or one you declare yourself. Whichever is appropriate, you assign the delegate to the event, which effectively registers the method that will be called when the event fires.


obj.MyEvent += new MyDelegate(obj.Display);

An event has the value null if it has no registered listeners. Although events are mostly used in Windows controls programming, they can also be implemented in console, web and other applications.

No comments:

Post a Comment