What is a delegate? Give a easy answer with an example
ThamizhJain
Delegates hold address of one or many functions. Delegates are similar to function pointers in c++. Delegate hides actual information like class names and function names. Delegates are 2 types: 1. Single cast delegate 2. Multi cast delegate Steps to create delegate: 1) write a class with methods Ex: Class Test { Public void print () { Messagebox.show ("from print"); } } 2) Create a delegate Public Delegate void Delegatename; 3) Create a variable to the delegate with the address of function Test t = new test (); Delegatename d = new Delegatename(t.print); 4) Invoke the delegate d ();
Delegates holds address of one or many functions. delegates are similar to function pointers in c++. delegate hides actual information like class names and function names. Delegates are 2 types: 1. single cast delegate 2. multi cast delegate steps to create delegate: 1) write a class with methods ex: Class Test { public void print() { Messagebox.show("from print"); } } 2) Create a delegate public Delegate void Delegatename; 3) create a variable to the delegate with the address of function Test t = new test(); Delegatename d = new Delegatename(t.print); 4)Invoke the delegate d();
Overview All of us have been exposed to event driven programming of some sort or the other. C# adds on value to the often mentioned world of event driven programming by adding support through events and delegates. The emphasis of this article would be to identify what exactly happens when you add an event handler to your common UI controls. A simple simulation of what could possibly be going on behind the scenes when the AddOnClick or any similar event is added to the Button class will be explained. This will help you understand better the nature of event handling using multi cast delegates. Delegates A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Call a Function directly - No Delegate In most cases, when we call a function, we specify the function to be called directly. If the class MyClass has a function named Process, we'd normally call it like this (SimpleSample.cs): using System; namespace Akadia.NoDelegate { public class MyClass { public void Process() { Console.WriteLine("Process() begin"); Console.WriteLine("Process() end"); } } public class Test { static void Main(string[] args) { MyClass myClass = new MyClass(); myClass.Process(); } } } That works well in most situations. Sometimes, however, we don't want to call a function directly - we'd like to be able to pass it to somebody else so that they can call it. This is especially useful in an event-driven system such as a graphical user interface, when I want some code to be executed when the user clicks on a button, or when I want to log some information but can't specify how it is logged. The very basic Delegate An interesting and useful property of a delegate is that it 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. The signature of a single cast delegate is shown below: delegate result-type identifier ([parameters]); where: * result-type: The result type, which matches the return type of the function. * identifier: The delegate name. * parameters: The Parameters, that the function takes. Examples: public delegate void SimpleDelegate () This declaration defines a delegate named SimpleDelegate, which will encapsulate any method that takes no parameters and returns no value. public delegate int ButtonClickHandler (object obj1, object obj2) This declaration defines a delegate named ButtonClickHandler, which will encapsulate any method that takes two objects as parameters and returns an int. A delegate will allow us to specify what the function we'll be calling looks like without having to specify which function to call. The declaration for a delegate looks just like the declaration for a function, except that in this case, we're declaring the signature of functions that this delegate can reference. There are three steps in defining and using delegates: * Declaration * Instantiation * Invocation A very basic example (SimpleDelegate1.cs): using System; namespace Akadia.BasicDelegate { // Declaration public delegate void SimpleDelegate(); class TestDelegate { public static void MyFunc() { Console.WriteLine("I was called by delegate ..."); } public static void Main() { // Instantiation SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc); // Invocation simpleDelegate(); } } } Compile an test: # csc SimpleDelegate1.cs # SimpleDelegate1.exe I was called by delegate ...