Delegates In .NET the concept of function pointers are implemented in the form of delegates. Unlike C function pointers the .NET delegates are type-safe. With the help of delegates you can pass one of the method as a argument to a function call. In creating threads you will pass the method as one of argument. In GUI environment for handling events also you can use it. In .NET Delegates are defined with delegate class. Using the delegates is a two stage process. Define the delegate Create one or more instances of the delegate. Delegates in C# always syntactically take one parameter constructor. The parameter being the method that delegate refers. You can invoke a delegate either by supplying brackets to delegate object on calling Invoke method on it.
Delegates You can apply any of the access modifier to delegate. publicdelegate void MyMethodInvoker(int x); //Also apply private, etc.., An instance of a given delegate can refers to any instance or static method on any object of any type, provided that the signature of method should math the signature of delegate. (So type-safety). Delegates are implemented as classes derived from the class System.MulticastDelegate which itself derived from System.Delegate. Delegate Inference: You can also create a delegate and assigning method to it as follow: MyMethodInvokerobjDelegate = MySquareFunc; //Delegate Inference Anonymous Methods: An anonymous method is a block of code that can be used as the parameter of the delegates instead of method. The syntax for defining delegate with anonymous method doesn’t change. While instantiating a delegate you need to supply code block instead of method name.
Multicast Delegates If you wrap a delegate with more then one method that delegate is called Multicast delegate. If a multi cast delegate is invoked, it will successively call each method in order they are registered with delegate. To make sense the delegate signature should return a void; A multicast delegate is a class derived from System.MulticastDelegate which in tern derived from System.Delegate. Let us define a delegate like below: delegate voidMathOpDelegate(double val); MathOpDelegate d1 = MathClass.MultiplyByTwo; MathOpDelegate d2 = MathClass.Square; MathOpDelegate mathOperations = d1 + d2; If one of the method invoked by a delegate thrown an exception, the complete iteration stops. To address this problem you can get list of member functions supported by multicast delegate and invoke them one by one.
Events Events in the .NET are based on delegate model. The event model in .NET Framework is based on having an event delegate that connects an event with its handler. To raise an event two events are needed.
A delegate that identifies the method that provides the response to the event.
A class that holds the event data. (Eg: EventArgs)
Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends the event is called as publisher and the class that receives the event called as subscriber. An event can have multiple subscribers. A subscriber can handle multiple events from multiple publishers. Events that have no subscribers are never called.
Events Inside the .NET framework, the EventHandler is a predefined delegate that specially represents event handler method for an event that does not generate data. Its definition inside .NET Framework as below: public delegate void EventHandler( Object sender, EventArgs e ) If your event does generate data then use EventHandler<TEvtArgs> generic delegate class and you can pass custom event data type as parameter. public delegate void EventHandler<TEventArgs>( Object sender, TEventArgs e ) where TEventArgs : EventArgs In .NET framework events are based on the EventHandler delegate and the EventArgs base class. public delegate void AlarmEventHandler(object sender, AlarmEventArgs e); These events delegates in the .NET framework has two parameters the source that raised the event and the data that raised the event. Similar to multicast delegates, event handlers can’t return a value always returns void. Because event handlers also wrap up more then one handler.
Generic Delegates With generic delegates the parameter of the delegate can be defined later. Simple delegate examples is given below: public delegate void Del<T>(T item); public delegateT2 Action<T1, T2>(T1 tVal); Nullable<T>: Suppose if you are instantiated as Nullable<int> x. The variable x can now be used like int and also you can assign null to it. Nullable<int> x; //or int? x; if(x.HasValue) x = 5; int y = x.Value; x += 20; x = null; EventHandler<TEventArgs>: With Windows forms, Web Applications delegates many different event handlers are defined. ArraySegment<T>: This structure is used to represent a segment of an array. The offset and count of segment is stored in this structure. Benefit of ArraySegment is you can pass it to method as a argument. Take note, ArraySegment don’t make copy of elements of array. It just refers it.