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