1) Delegate is a function pointer.It holds the address of a method.It is a reference type and holds the reference of a method. 2) The method name must have same signature with delegates.2 Types 1) Single Cast Delegate : Delegate object must be referenced with single method.Ex :public delegate int Singlecast(int x, int y)class Test{public int Add(int x,int y){return x y;}static void main() {Singlecast d = new Singlecast(Test.Add)d(10,5); } }Multicast Delegates : Same signature must have multiple method names.public delegate Multicast(int x,int y)class Test {public int Add(int x,int y){return x y;} public int Mul(int x,int y){return x *y;}static void main() {Multicast m =new Multicast();m.Add();m.Mul();m(10,5); }}
Here i have explain simple example of delegate. http://aspdotnetmyblog.blogspot.in/2013/07/delegate-with-example-in-c.html
http://msdn.microsoft.com/en-us/library/aa288459(v=vs.71).aspx
C# delegates are similar to pointers to functions, in C or C++. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime.
Delegates are especially used for implementing events and the call-back methods. All delegates are implicitly derived from the System.Delegate class.
Refer to a method, which have the same signature as that of the delegate.
For example, consider a delegate:
public delegate int MyDelegate (string s);
Following example demonstrates declaration, instantiation and use of adelegate that can be used to reference methods that take an integer parameter and returns an integer value.
using System;delegate int NumberChanger(int n); namespace DelegateAppl { class TestDelegate { static int num = 10; public static int AddNum(int p) {num += p; return num; } public static int MultNum(int q) {num *= q; return num; } public static int getNum() { return num; }
static void Main(string[] args) { //create delegate instances NumberChanger nc1 = new NumberChanger(AddNum); NumberChanger nc2 = new NumberChanger(MultNum); //calling the methods using the delegate objectsnc1(25); Console.WriteLine("Value of Num: {0}", getNum());nc2(5); Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } } }