---
title: "Why are use the delegates in C# and how to pass a parameter in delegate, also define the benefits."  
description: "Why are use the delegates in C# and how to pass a parameter in delegate, also define the benefits."  
author: "Ethan Karla"  
published: 2021-08-26  
canonical: https://answers.mindstick.com/qa/93930/why-are-use-the-delegates-in-c-sharp-and-how-to-pass-a-parameter-in-delegate-also-define-the-benefits  
category: "programming"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Why are use the delegates in C# and how to pass a parameter in delegate, also define the benefits.

Why are use the [delegates in C#](https://www.mindstick.com/forum/33765/delegates-in-c-sharp) and how to pass a [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) in [delegate](https://www.mindstick.com/articles/23281/c-sharp-delegates), also [define](https://answers.mindstick.com/qa/72286/for-define-what-rats-and-rabbits-use-their-claws) the benefits.

## Answers

### Answer by Ravi Vishwakarma

The **C# delegates** are like the function pointer present in C, C++, as you must have read pointers in C, C++ Normally the job of pointers is to hold the reference. The C# delegate is like a reference type variable that holds the reference to a method. And this reference can change at runtime. All delegates derive from System. Delegate Class. If you want to use delegate then you have to use the delegate keyword. **Syntax**

```
access-modifier delegate return type delegate-name(parameters)
```

**Example**

```
using System;public class Program
{
 //declare delegate
 public delegate void  DelegateFunction(int a, int b);
 public void Sum(int a, int b){
  Console.WriteLine('Sum : '+ (a+b));
 }
 public void Sub(int a, int b){
  Console.WriteLine('Substraction : '+ (a-b));
 }
 public void Mulifly(int a, int b){
  Console.WriteLine('Mulification : '+ (a*b));
 }
 public static void Main()
 {
  Program pr=new Program();
  //adding sum function in delegate
  DelegateFunction delfun=pr.Sum;
  delfun(45,12);
  //adding sub function in delegate
  delfun=pr.Sub;
  delfun(45,12);
  //adding Mulifly function in delegate
  delfun=pr.Mulifly;
  delfun(45,12); }
}
```

**Output**

```
Sum : 57
Substraction : 33
Mulification : 540
```

\
Delegates are also called type-safe function pointers because the return type of the function is the same as the return type delegate. **We can also pass the delegate as a parameter**, A method can also have a parameter of the delegate type.

```
access-modifier return-type fuction-name(delegate-name delegate-reference, arguments1, arguments2, … ){ delegate-reference(arguments1, arguments2, …)}
```

**Example**

```
using System;public class Program
{
 //declare delegate
 public delegate void  DelegateFunction(int a, int b);
 public void Sum(int a, int b){
  Console.WriteLine('Sum : '+ (a+b));
 }
 public void Sub(int a, int b){
  Console.WriteLine('Substraction : '+ (a-b));
 }
 public void Mulifly(int a, int b){
  Console.WriteLine('Mulification : '+ (a*b));
 }
 // delegate as parameter
 public void DelegateAsParameter(DelegateFunction delegatefunction, int a, int b)
 {
  delegatefunction(a,b);
 }
 public static void Main()
 {
  Program pr=new Program();
  pr.DelegateAsParameter(pr.Sum,45,12);
  pr.DelegateAsParameter(pr.Sub,45,12);
  pr.DelegateAsParameter(pr.Mulifly,45,12);
 }
}
```

**Output**

```
Sum : 57
Substraction : 33
Mulification : 540
```

**Multicast Delegates** A delegate can point to more than one method, this means that a delegate can keep the address of more than one method and call them one by one. That is, a delegate that points to multiple methods is called a multicast delegate. **Example**

```
using System;public class Program
{
 //declare delegate
 public delegate void  DelegateFunction(int a, int b);
 public void Sum(int a, int b){
  Console.WriteLine('Sum : '+ (a+b));
 }
 public void Sub(int a, int b){
  Console.WriteLine('Substraction : '+ (a-b));
 }
 public void Mulifly(int a, int b){
  Console.WriteLine('Mulification : '+ (a*b));
 }
 // delegate as parameter
 public void DelegateAsParameter(DelegateFunction delegatefunction, int a, int b)
 {
  delegatefunction(a,b);
 }
 public static void Main()
 {
  Program pr=new Program();
  // Multicast Delegates adding one by function in one delegate
  DelegateFunction dfun=pr.Sum;
  dfun+=pr.Sub;
  dfun+=pr.Mulifly;
  // calling the delegate
  dfun(45,12);
 }
}
```

**Output**

```
Sum : 57
Substraction : 33
Mulification : 540
```


---

Original Source: https://answers.mindstick.com/qa/93930/why-are-use-the-delegates-in-c-sharp-and-how-to-pass-a-parameter-in-delegate-also-define-the-benefits

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
