Intro blah...blah...
Delegate is an object which is used to call the method in C#. Basically delegate is nothing but a type reference which holds a reference of the method. Delegate is similar to callback function in C. C uses the function pointer to refer a function. Same way C# also used two concept to satisfy the delegates, that are given below:
Delegate type: It defines the syntax of the delegate. It defines the methods return type and number of parameters and its type. For example:
Delegate instance: Delegate variable holds an address of the method, creates delegate instance. For example:
I hate theory...
Fine, Lets try with some example.
delegate int Calculate(int x, int y); // Create delegate type
class MyMath
{
static void main() {
Calculate t = add; // Create delegate instance
int result = t(5, 4); // Invoke delegate
Console.WriteLine(result); // 9
// Change the delegate instance dynamically
t = subtract;
result = t(5, 4);
Console.WriteLine(result); // 1
}
static int add(int x, int y) {
return x + y;
}
static int subtract(int x, int y) {
return x - y;
}
}
Calculate is a delegate type, which compatible with any method whose return type as well as parameters. The method add and subtract is exactly match with delegate type. So 't' can create delegate instance with add as well as subtract method.
Multicast Delegates
This is the very important feature in C# over C. All the delegates can have the multicast capability. C# delegates can have a reference not just a single method, It can able to hold list of methods. The operators like +, -, +=, and -= are used to add or delete the delegates from the list. For example:
Calculate t = add;
t += subtract;
Now invoking t will call both 'add' and 'subtract' methods. But if the delegate is having non-void return type then the caller will receive the return value from the last method. The delegates are called in the order they are added.
All the delegates are derived from System.MulticaseDelegate, which is derived from System.Delegate. The example for multicast delegate is given below.
delegate int Calculate(int x, int y); // Create delegate type
class MyMath
{
static void main() {
Calculate t = add; // Create delegate instance
t += subtract; // Multicast delegate
t += multiply;
t(5, 4); // Invoke delegates
t -= subtract;
t(3, 4); // Invoke delegates
}
static void add(int x, int y) {
Console.WriteLine(x + y);
}
static void subtract(int x, int y) {
Console.WriteLine(x - y);
}
static void multiply(int x, int y) {
Console.WriteLine(x * y);
}}
Conclusion
I believe this article is simple enough to understand the delegates in C#. If you have any query regarding this article feel fee to post a comment or contact me through my mail.
Delegate is an object which is used to call the method in C#. Basically delegate is nothing but a type reference which holds a reference of the method. Delegate is similar to callback function in C. C uses the function pointer to refer a function. Same way C# also used two concept to satisfy the delegates, that are given below:
Delegate type: It defines the syntax of the delegate. It defines the methods return type and number of parameters and its type. For example:
delegate int Calculate(int x, int y);
Delegate instance: Delegate variable holds an address of the method, creates delegate instance. For example:
Calculate t = add ;
I hate theory...
Fine, Lets try with some example.
delegate int Calculate(int x, int y); // Create delegate type
class MyMath
{
static void main() {
Calculate t = add; // Create delegate instance
int result = t(5, 4); // Invoke delegate
Console.WriteLine(result); // 9
// Change the delegate instance dynamically
t = subtract;
result = t(5, 4);
Console.WriteLine(result); // 1
}
static int add(int x, int y) {
return x + y;
}
static int subtract(int x, int y) {
return x - y;
}
}
Calculate is a delegate type, which compatible with any method whose return type as well as parameters. The method add and subtract is exactly match with delegate type. So 't' can create delegate instance with add as well as subtract method.
Multicast Delegates
This is the very important feature in C# over C. All the delegates can have the multicast capability. C# delegates can have a reference not just a single method, It can able to hold list of methods. The operators like +, -, +=, and -= are used to add or delete the delegates from the list. For example:
Calculate t = add;
t += subtract;
Now invoking t will call both 'add' and 'subtract' methods. But if the delegate is having non-void return type then the caller will receive the return value from the last method. The delegates are called in the order they are added.
All the delegates are derived from System.MulticaseDelegate, which is derived from System.Delegate. The example for multicast delegate is given below.
delegate int Calculate(int x, int y); // Create delegate type
class MyMath
{
static void main() {
Calculate t = add; // Create delegate instance
t += subtract; // Multicast delegate
t += multiply;
t(5, 4); // Invoke delegates
t -= subtract;
t(3, 4); // Invoke delegates
}
static void add(int x, int y) {
Console.WriteLine(x + y);
}
static void subtract(int x, int y) {
Console.WriteLine(x - y);
}
static void multiply(int x, int y) {
Console.WriteLine(x * y);
}}
Conclusion
I believe this article is simple enough to understand the delegates in C#. If you have any query regarding this article feel fee to post a comment or contact me through my mail.