Delegate
Delegate is similar to function pointer in C and C++. A function pointer in C is a variable that points to function. Similarly, a delegate is a reference type in .net that is used to call the methods of similar signature as of the delegate. delegates are type-safe, object oriented.
Types of Delegate:
Single cast delegate
SingleCast Delegates refer to a single method with matching signature. SingleCast Delegates derive from the System.Delegate class. In single-cast delegates, the delegate can point to both static and non-static method if both have the same signature as the delegate
simple delegate with no return type and taking two arguments as parameters.
public delegate void MyMethod1(string s ,int i);
simple delegate that returns a value.
public delegate bool MyMethod2());
Small Example:
// Decalre Delegate which will point to a method which takes string as parameter and returns string as well public delegate string MyOwnDelegate(string message); // Decalre Delegate which will point to a method which takes 2 integer parameters and returns integer as well public delegate int MyOwnAddDelegate(int a, int b);
//The functions to which delegates pointing public string MessageMethod(string name) { MessageBox.Show(name); return "Hi" + name; } public int AddNumber(int a, int b) { return (a + b); }
// We have created an instance of our MyOwnDelegate & MyOwnAddDelegate
MyOwnDelegate myStringDelegate = new MyOwnDelegate(MessageMethod); MyOwnAddDelegate myAddDelegate = new MyOwnAddDelegate(AddNumber);
// when we call the delegate it calls the method which it is pointing to string result1 = myStringDelegate("Hello"); int result2 = myAddDelegate(10,20);
Multi cast delegate
A delegate is called Multi-cast Delegate that derives from the System.MulticastDelegate contains an invocation list with multiple methods.
public delegate void MyMulticastDelegate(string name);
public void DisplayFirstName(string firstName) { MessageBox.Show(firstName); } public void DisplaySecondName(string secondName) { MessageBox.Show(secondName); }
MyMulticastDelegate myDelegate = new MyMulticastDelegate(DisplayFirstName); myDelegate += new MyMulticastDelegate(DisplaySecondName); myDelegate("Hima Sagar");
Anonymous methodS anonymous method is a method without any name.Anonymous methods can be used in the place where there is a use of a delegate.For example there is a mthod, wouldn’t be called by any other code other than the delegate itself then we can make use of anonymous method i.e. method with no name.
public delegate string MyOwnDelegate(string message); public delegate int MyOwnAddDelegate(int a, int b);
public MyOwnDelegate myStringDelegate = delegate(String name) { MessageBox.Show(name); return "Hi" + name; };
public MyOwnAddDelegate myAddDelegate = delegate(int a, int b) { return (a + b); }; string result3 = myStringDelegate("Hello"); int result4 = myAddDelegate(10, 20);
For more details please go through the following websites http://msdn.microsoft.com/en-us/library/0yw3tz5k(VS.80).aspx http://msdn.microsoft.com/en-us/library/ms173172(VS.80).aspx
Himasagar Kutikuppala
|
No responses found. Be the first to respond and make money from revenue sharing program.
|