Introduction
In C# and .NET, many developers struggle to build a conceptual understanding of delegates. In my team, during presentations, many people ask to discuss delegate. Once my senior at the Pune office said that when C# comes to delegates, it looks like a new programming language. Delegates can be compared to function pointers in C++ but they have many other concepts based on it. In this article, I will talk about what a delegate is and how you can extend its concept in the world of C# and . NET.
Background
Delegates are heavily used in Events, LINQ, .NET v 3.5 onwards, extension methods, and ASP.NET MVC, etc. I will give you a very comprehensive explanation about delegates that is based on etymologies, like the Anonymous Method and Lambda Expression. Actually, there are a lot of other things that can be discussed pertaining to delegate but that will be a separate discussion and I shall discuss that part possibly another time. In this article, you will get knowledge about three major concepts very easily.
- Delegates have been a part of C# since version 1.0 was released. A delegate holds a reference to a method. Delegates can be defined in two types: Singlecast and Multicast (Both types are discussed below in code).
- The Anonymous Method is an inline code that can be used wherever a delegate type is expected. Microsoft introduced Anonymous Methods in C# 2.0 somewhere around 2003.
- A lambda expression is an anonymous method that you can use to create delegates or expression tree types. Microsoft introduced Lambda Expression in C# 3.0 sometime in 2007. Most of the time, anonymous methods are confused with lambda expressions by many developers. I have discussed this in detail here. You know that there is one case in which an anonymous method provides functionality not found in lambda expressions, as anonymous methods enable you to omit the parameter(s).
Using Code
using System;
namespace DAL
{
public delegate void MyDelegate();
class Program
{
public void CallerOne()
{
Console.WriteLine("This is Caller One function...");
}
static void Main()
{
// Create an instance of the class
Program p = new Program();
// Create an instance of the delegate and pass the method as a parameter
MyDelegate myDelegate = new MyDelegate(p.CallerOne);
myDelegate();
}
}
}
You can see that I have a delegate named MyDelegate and as you know, it can hold a reference to a Method. So, MyDelegate holds a reference to a Method CallerOne(). Know that you can also execute the method with myDelegate.Invoke(); instead of calling delegate object myDelegate();. This definition will remain similar in the following code too. One more point to note - Here, look at the return type and parameters. Delegate and Method signatures must be the same to implement it. Let's go next to copy and paste the below code into your console application's Program.cs.
using System;
namespace DAL
{
public delegate void MyDelegate();
class Program
{
public void CallerOne()
{
Console.WriteLine("This is Caller One function...");
}
public void CallerTwo()
{
Console.WriteLine("This is Caller Two function...");
}
static void Main()
{
// Create an instance of the class
Program p = new Program();
// Create an instance of the delegate and pass the method as a parameter
MyDelegate myDelegate = new MyDelegate(p.CallerOne);
// Create another instance of the delegate and pass a different method as a parameter
MyDelegate myDelegate1 = new MyDelegate(p.CallerTwo);
// Invoke the delegate instances
myDelegate();
myDelegate1();
}
}
}
In the above step, there is another object of Delegate named myDelegate1 but it does a similar job as in the first code. So, we have implemented two objects of the same delegate. Let's next copy and paste the below code in your console application's Program.cs.
using System;
namespace DAL
{
public delegate void MyDelegate();
class Program
{
public void CallerOne()
{
Console.WriteLine("This is Caller One function...");
}
public void CallerTwo()
{
Console.WriteLine("This is Caller Two function...");
}
public static void CallerThree()
{
Console.WriteLine("This is Caller Three function...");
}
static void Main()
{
// Create an instance of the class
Program p = new Program();
// Create an instance of the delegate and pass the method as a parameter
MyDelegate myDelegate = new MyDelegate(p.CallerOne);
// Create another instance of the delegate and pass a different method as a parameter
MyDelegate myDelegate1 = new MyDelegate(p.CallerTwo);
// Create another instance of the delegate and pass a static method as a parameter
MyDelegate myDelegate2 = new MyDelegate(CallerThree);
// Invoke the delegate instances
myDelegate();
myDelegate1();
myDelegate2();
}
}
}
Seth SchaffnerPosted Jan 12, 2023, 6:40 PM
The section before the code blocks was very helpful. After the code blocks, there were so many typo that it became difficult to read. Here is the last section with corrections: You can take note that you have passed parameter num with value 211, but the Anonymous Method can omit it and the program is still executed. Now, with the same delegate, look at the below code which uses a Lambda Expression. It will throw a compile time error because you will have to pass a parameter in method body to compile and execute it. How do you pass the parameter in? I leave this exercise up to you. Post your answer in the comments.
Joe WilsonPosted Jan 26, 2017, 5:40 AM
Thank you for sharing it.