Introduction to C# delegate and multicast delegate
Tags: csharp, dotnet, programming, tutorial
Published: 2021-02-16 02:22:40

<!-- wp:heading -->
<h2>What is a C# delegate?</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The C# delegate is a reference data type that defines the method and their signature. The delegate stores the address of one or more methods. When a delegate for a method assigned, it behaves exactly like the method. It can be invoked like any other method with parameters and return value.<br>A delegate simply stores the reference of the methods with the same signatures such as return type and parameter list. A delegate can be invoked as like a class by creating an object for the delegate. The delegate object allows running the methods at run time without invoking it directly.</p>
<!-- /wp:paragraph -->
This post was originally posted on [DevelopersDoors](https://developersdoors.xyz)

<!-- wp:heading -->
<h2>Usage of delegate:</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>When an event needs to be used then delegates can be used. Multicast delegates can be used for invoking many events at a time. When a class need to have more than one implementation of the method and to encapsulate a static method.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Refer for when to use delegates and interfaces -&gt; <a href="https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ms173173(v=vs.100)" target="_blank" rel="noreferrer noopener">https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/ms173173(v=vs.100)</a></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Declaring a delegate:</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The <code>System.Delegate</code> class is the base class for the delegate declaration. The delegate keyword is used to declare a delegate. The return type and parameters should same as the methods.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Eg:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>public delegate int CalDelegate(int num1,int num2);</code></p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Instantiation and invoking delegates:</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To instantiate a delegate, an instance is created similar to how a class instance is created. When a delegate is instantiated, the address of the method is stored in the instance.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><br>Eg:<br><code>Calculator cal=new Calculator();<br>CalDelegate sumDel=new CalDelegate(cal.sum);<br>CalDelegate mulDel=new CalDelegate(cal.multiply);</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><br><code>CalDelegate</code> is the delegate created and the <code>sumDel</code> is the delegate instance instantiated to<code> CalDelegate</code> with the reference to the <code>sum()</code> method in cal instance and <code>mulDel</code> is for reference of <code>multiply()</code> method.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><br>To invoke the delegate, calling the delegate instance with the parameter list referenced by the delegate with the same data type.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><br>Eg:<br><code>sumDel(10, 12);</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>mulDel(10,12);</code></p>
<!-- /wp:paragraph -->
```C#
using System;

public delegate int CalDelegate(int num1,int num2);
public class Calculator
{
	public int sum(int num1,int num2)
	{
		return num1+num2;
	}
	public int multiply(int num1,int num2)
	{
		return num1*num2;
	}
}
public class Program
{
	public static void Main()
	{
		Calculator cal=new Calculator();
		CalDelegate sumDel=new CalDelegate(cal.sum); // sumDel for sum() reference
		CalDelegate mulDel=new CalDelegate(cal.multiply); //mulDel for multiply() reference
		Console.WriteLine(sumDel(10,12));
		Console.WriteLine(mulDel(10,12));
	}
}

```
<!-- wp:paragraph -->
<p>Output:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>22</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>120</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Multicast delegates:</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To have a reference of more than one method in delegate multicast delegates is used. This mainly used in the multi-event invocation. The += operator is used to add the methods referred to delegate and -= operator is used to remove the methods. The methods are called in First-In-First-Out(FIFO) order.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Remember when using multicast delegates use void as return types because the use of multiple methods will clash in return type. So void is safe to avoid runtime exception.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Eg:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>CalDelegate simpleDel=new CalDelegate(cal.sum);</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>simpleDel += new CalDelegate(cal.multiply);</code></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>simpleDel -= new CalDelegate(cal.sum);</code></p>
<!-- /wp:paragraph -->

```C#
using System;

public delegate void CalDelegate(int num1,int num2);
public class Calculator
{
	public void sum(int num1,int num2)
	{
		Console.WriteLine(num1+num2);
	}
	public void multiply(int num1,int num2)
	{
		Console.WriteLine(num1*num2);
	}
}
public class Program
{
	public static void Main()
	{
		Calculator cal=new Calculator();
		CalDelegate simpleDel=new CalDelegate(cal.sum);
		simpleDel+=new CalDelegate(cal.multiply); //multicasting using += operator
		simpleDel(10,12);
	}
}

```
<!-- wp:paragraph -->
<p>Output:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>22</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>120</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The above delegate example is used with multicast delegates in which the first<code>sum()</code>method is referenced to the<code>simpleDel</code>delegate. Then<code>multiply()</code>method is added to multicast using <code>+=</code> operator. To remove the methods from delegate simply use <code>-=</code> operator. As said multicast delegate run in FIFO, first<code>sum()</code>delegate reference is run and then<code>multiply()</code>with output 22 and 120 respectively.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For documentation of delegates refer -&gt; <a href="https://docs.microsoft.com/en-in/dotnet/api/system.delegate?view=netcore-3.1" target="_blank" rel="noreferrer noopener">https://docs.microsoft.com/en-in/dotnet/api/system.delegate?view=netcore-3.1</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In next post, I will explain synchronous delegate, asynchronous delegate and covariance and contravariance delegates.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For my other C# archives -&gt; <a href="https://developersdoors.xyz/category/programming/c/" target="_blank" rel="noreferrer noopener">Go</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you enjoy my post please share it and if any suggestion please comment on it.</p>
<!-- /wp:paragraph -->