what is delegates and why we use delegates?explain in detail
c#
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ajay YadavPosted Oct 27, 2014, 10:01 PM
A Delegate is an abstraction of one or more function pointers. The .NET has implemented the concept of function pointer in form delegate .With delegates; you can treat a function as data. Delegates allow function to be passed as parameters, returned from a function as value and stored in an array. The delegate has following characteristics.
§ Delegates are derived from System.MulticastDelegate class.
§ They have a signature and a return type. A function that is added to delegates must be compatible with this signature.
§ Delegates can point either static or instance methods.
§ Once a delegate object has been created, it may dynamically invoke the methods it points to at runtime.
§ Delegates can call methods synchronously and asynchronously.
The delegate contains a couple of useful fields. The first one holds a reference to an object, and the second holds a method pointer. When you invoke the delegate, the instance method is called on the contained reference. However, if the object reference is null, the runtime understands this to mean that the method is a static method. Moreover, invoking a delegate syntactically is the exact same as calling a regular function. Therefore, delegates are perfect for implementing callbacks.
Yadagiri ReddyPosted Oct 27, 2014, 3:39 PM
Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates. You create a custom method, and a class such as a windows control can call your method when a certain event occurs. The following example shows a delegate declaration:
public delegate int PerformCalculation(int x, int y);
refer this..
http://msdn.microsoft.com/en-IN/library/ms173171.aspx