Let’s see the delegate concept in C# and .NET with the details of the information.
A few important points to remember about delegate in C#.NET.
1. With the help of delegate in C# and .NET, we can achieve the function pointer/Callback functionality concept of C++.
2. Advantage C# and .NET delegate over C++ function pointer is a delegate, which includes type-safe information.
3. Type information like a) Number of parameters.
- Types of parameters
- Calling convention
- Return type
4. Mainly delegate is used in C# and .NET for two purposes, which are given below.
- Callback &
- Event Handling
a. Let us see: When we call any of the functions but when this function doesn’t return anything back to the calling function, in such cases it is something like a one-way communication.
b. Callback function: When we call a function but when this function returns something back to the calling function in such cases, it is something like a two-way communication; i.e., nothing but a callback function.
5. Delegate functionality is provided through the System. Delegate Namespace.
6. Actually delegate is a class, which holds the references to the methods or the functions.
7. There are different kinds of delegates.
- Single Cast delegate.
- In this type, it has only one method in its invocation list.
- Multicast delegates.
- In this type, it has one or more than one method/method in its invocation list.
- Again, there are two types of Multicast delegates.
- Multicast delegate with void return types.
- Multicast delegate with non-void return types.
8. About the delegate method: The delegate method is a method, whose reference is encapsulated in the delegate instance.
9. About delegate instance: It is actually an object of the delegate class.
Now, let us start.
First of all, we will look into it.
Single Cast delegate
The code snippet is given below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Sandip Patil Practices
namespace delegateDemoApp
{
class DemoApp
{
public delegate int ArithOp(int x, int y);
class MathOps
{
public static int Add(int a, int b)
{
return a + b;
}
public static int Mul(int a, int b)
{
return a * b;
}
}
static void Main(string[] args)
{
ArithOp Op1 = new ArithOp(MathOps.Add);
ArithOp Op2 = new ArithOp(MathOps.Mul);
int k1 = Op1(10, 20);
int k2 = Op2(10, 20);
Console.WriteLine("Addition: " + k1);
Console.WriteLine("Multiplication: " + k2);
Console.Read();
}
}
}
Explanation
In the above program.
- The declaration of delegate is given in the code line given below.
public delegate int ArithOp(int x, int y); - A delegate method is given below since it matches the signature of the delegate.
- The code line given below demonstrates how Add is a delegate method.
public static int Add(int a, int b) { return (a + b); } - Instantiation of delegate
- The code line given below demonstrates it.
ArithOp Op1 = new ArithOp(MathOps.Add); - Now, here MathOps.Add is a delegate method since its reference is encapsulated in delegate instance Op1.
- Invocation of the delegate method
- The code line given below demonstrates it.
- Invocation of MathOps.Add as a delegate method.
int k1 = Op1(10, 20); - It returns an integer value.
The output of the above program is given below.
Addition 30
Multiplication 200
Now, let us see.
How to create an array of delegate instances?
The code snippet is given below for it.

Tejasvini PatilPosted Feb 20, 2017, 7:40 PM
Very useful and simply explained.. keep sharing .Thank you
JAYDIP PATILPosted Feb 15, 2017, 12:56 PM
Nice article.........Well explained
Akshay AroraPosted Feb 15, 2017, 4:02 AM
Well explained its really worth , Thankyou .
Anu VPosted Feb 7, 2017, 11:20 PM
Thanks for sharing.. Nice article..
Pravas RanjanPosted Feb 2, 2017, 1:28 AM
Hello Sir can you please add some topic on ADO.Net and Entity framework with brief details so that it can very help full for us and thanks again for effort here to deliver this kind of information to us.....
Sandip G PatilPosted Jan 31, 2017, 8:35 AM
Thanks ........................... a lot...........
Pravas RanjanPosted Jan 31, 2017, 7:10 AM
Your way of explanation is excellent sir....
Pravas RanjanPosted Jan 31, 2017, 7:08 AM
Excellent Article Sir...keep posting more article like this sir...
Manav PandyaPosted Jan 20, 2017, 12:11 PM
Nice article share sir ji sandip