A delegate in C# is similar to 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. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure.
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.
In simple terms a delegate is a data type that defines kinds of methods and explains events(which uses delegates),exception, and error handling. A delegate is a data type much similar to a class or a structure. In contrast , a delegate is a type that defines the parameters and returns values of the method.
Understanding the syntax :
The following code shows you can define a delegate type.
[accessibility] delegate returnType DelegateName ([parameters]);
Now lets break the code:
Accessibility : An accessibility for the delegate type such as public or private.
Delegate : The delegate keyword.
returnType : The data type that a method of this delegate type returns such as void , int , string.
DelegateName : The name that you want to give to the delegate.
Parameter : The paramenter list that a method of this delegate type should take.
Let’s understand this by the following example code:
The following example illustrates declaring , instantiating , and using a delegate. The class “MyFirstClass” have 2 methods Add and Length. Add simply concatenates the first name and last name of the object formed in main, and another method called Length that simply returns the length of the string entered of the first name and last name.
The delegate type used is called “MyFirstDelegate” . If we have more than one namespaces and we have classes and want to access the functionality of both the classes having same number of signatures , we can do it by using a delegate.
read complete article here : http://blog.mycampusnotes.com/working-delegates-c/