A Delegate is a very important topic in C# programming. In almost all C# interviews, we face these questions.
So let’s understand this topic well so that we can tell anyone about the delegate at any time.
What is a Delegate in C#?
In simple words, Delegate holds the reference of a method. Please don’t get confused, as we are going to elaborate more about delegate.
A delegate is the same as a function pointer in C or a pointer in C++. Don’t worry let’s define the delegate more concisely now.
A delegate is an object and it holds the reference of a method. So, we can pass a method as a parameter to a different method using a delegate.
So far, we are ok with defining delegate. Now, we will understand delegate in C# with the code snippet.
Delegate Syntax in C#
Let’s look at the syntax:
public delegate int DelegateName(string value)
Here, public
is the access specifier int
is the return type of delegate and it accepts string
parameter. The name of the delegate is DelegateName
.
How to Invoke a Delegate in C#?
We will see the delegate in action with the help of C# code snippet:
public class Product
{
public delegate decimal ProductPriceDelegate(decimal cost, decimal taxes);
public decimal ProductPriceWithTaxes(decimal cost, decimal tax)
{
return cost + (cost*18/100);
}
}
Here, we have a class Product
and this class contains a delegate with the name ProductPriceDelegate. Next, we have a method in this class which accepts 2 parameters and returns a decimal value.
To invoke delegate, let’s see the code snippet:
var product = new Product();
var productPrice = new ProductPriceDelegate(product.ProductPriceWithTaxes);
decimal result = productPrice(500, 18);
Console.WriteLine("Product Price with taxes: {0}", result);
Console.ReadLine();
First, we create the object of the class Product
. Then, we instantiate the delegate which is ProductPriceDelegate
and finally, we pass the parameters to the productPrice
.
So, here we are passing the method as a parameter to the delegate ProductPriceDelegate()
and finally invokes the delegate.
Types of Delegate
In C#, we have 2 types of delegates.: Singlecast and multicast delegate.
Single-cast Delegate
A delegate that holds the reference to only one method is a Single-cast delegate. In other words, Single-cast delegates can invoke a single method. So far, we have seen single-cast delegates only.
Syntax to declare a delegate:
public delegate void DoSomething(int number);
Syntax to invoke a Singlecast delegate:
var delegateVar = new DoSomething(Method1);
Here, we pass the Method1()
to the delegate DoSomething()
. As it holds the reference of a single method, so we call it Singlecast Delegate.
Multicast Delegate
In C#, it is possible to hold the reference of two or more methods, this type of Delegate is called a Multi-cast delegate.
Let’s see multicast delegate in action:
public class MultiCastDelegate
{
public delegate decimal ProductTotalPriceDelegate(decimal cost, decimal taxes);
public decimal ProductPriceWithoutTax(decimal cost, decimal tax = 0)
{
Console.WriteLine("Method1");
decimal result = cost;
return result;
}
public decimal ProductPriceWithTaxes(decimal cost, decimal tax)
{
Console.WriteLine("Method2");
decimal result = cost + (cost * tax / 100);
return result;
}
}
Here, we have a class MultiCastDelegate
and this class contains 1 delegate and 2 methods. We have set the delegate name to ProductTotalPriceDelegate()
and it accepts 2 parameters of type decimal.
So, let’s invoke the delegate and see how multicast delegate works:
MultiCastDelegate multiCastDelegate = new MultiCastDelegate();
ProductTotalPriceDelegate multicastDelegateProductCost = new ProductTotalPriceDelegate(multiCastDelegate.ProductPriceWithoutTax);
//This is multicast delegate
multicastDelegateProductCost += new ProductTotalPriceDelegate(multiCastDelegate.ProductPriceWithTaxes);
decimal result = multicastDelegateProductCost(500,10);
Console.WriteLine("Product Price: {0}", result);
Console.ReadLine();
Firstly, we are creating the object of MultiCastDelegate
class. Secondly, we are instantiating the ProductTotalPriceDelegate()
delegate.
Now, we pass the method ProductPriceWithoutTax()
to the delegate and then the ProductPriceWithTaxes()
method to the same delegate. So, in this way, the ProductTotalPriceDelegate()
delegate is a multicast delegate as it holds the reference of two methods.
Note: A multicast delegate returns the value from the last target method.
Let’s run and see the output:
Method1
Method2
Product Price: 550
Why is Delegate useful in C#?
As we know, we use delegate to pass a method as a parameter. It is useful to tell which method to call when an event occurs. So, we can use a delegate to define callback methods. We can use multicast delegates to create a chain together. For example, a delegate is useful to call multiple methods on a single event.
Apart from these benefits, the use of delegates makes our code cleaner and simpler.
Summary
In this article, we understand about Delegate and types of Delegate. Using a delegate, we can use a method as a value and pass it as a normal parameter. In other words, a delegate can hold the reference of a method. If it holds the reference of a single method, we call it a Singlecast delegate. And, if it holds the reference of 2 or more methods then we call it a Multicast delegate.
Keep following: SharePointCafe.NET