---
title: "Delegate in C#"  
description: "Basically it is similar like the old \"C\" age function pointer, where functions can be assigned like a variable and called in the run time based on dyn"  
author: "mohan kumar"  
published: 2012-06-23  
updated: 2017-11-29  
canonical: https://www.mindstick.com/articles/951/delegate-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Delegate in C#

##### So what is delegate?

Basically it is similar like the old "C" age [function](https://www.mindstick.com/articles/43970/purchase-suitable-wedding-dresses-for-your-wedding-function) pointer, where [functions](https://www.mindstick.com/forum/34086/jquery-callback-functions) can be assigned like a [variable](https://www.mindstick.com/blog/11493/what-is-a-variable) and called in the run time based on dynamic conditions. C# [delegate](https://www.mindstick.com/articles/777/delegate-and-event-in-c-sharp) is the smarter version of function pointer which helps [software](https://yourviews.mindstick.com/view/88275/why-must-software-beginners-join-the-mindstick-training-program-to-enhance-coding-skills) architects a lot, especially while utilizing [design patterns](https://www.mindstick.com/articles/337478/the-role-of-design-patterns-in-enhancing-code-reusability).

At first, a delegate is defined with a specific [signature](https://www.mindstick.com/interview/516/explain-about-xml-signature) (return type, [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) type and order etc). To invoke a delegate object, one or more methods are [required](https://yourviews.mindstick.com/view/81031/vigilence-required-during-corona-pandemic) with the EXACT same signature. A delegate object is first created similar like a class object created. The delegate object will basically hold a [reference](https://www.mindstick.com/forum/34619/call-by-value-and-call-by-reference) of a function. The function will then can be called via the delegate object.

Sounds easy? If not let’s have a look in the code snippets below.

##### 1. Defining the delegate

```
 public delegate int   Calculate  (int value1, int value2);
```

##### 2. Creating methods which will be assigned to delegate object

```
//a method, that will be assigned to delegate objects //having the EXACT signature of the delegatepublic int  add(int value1, int value2) {      return value1 + value2;              } //a method, that will be assigned to delegate objects //having the EXACT signature of the delegate public int sub(  int value1, int value2) {      return value1 - value2;              } {     return value1 + value2;             }//a method, that will be assigned to delegate objects //having the EXACT signature of the delegatepublic int sub(  int value1, int value2) {     return value1 - value2;             }
```

##### 3. Creating the delegate object and assigning methods to those delegate objects

```
//creating the class which contains the methods  //that will be assigned to delegate objectsMyClass  mc = new MyClass();  //creating delegate objects and assigning appropriate methods //having the EXACT signature of the delegate Calculate add =  new Calculate(mc.add); Calculate sub = new Calculate(mc.sub);//creating delegate objects and assigning appropriate methods //having the EXACT signature of the delegateCalculate add =  new Calculate(mc.add);Calculate sub = new Calculate(mc.sub);
```

##### 4. Calling the methods via delegate objects

```
 //using the delegate objects to call the assigned methods Console.WriteLine("Adding two values: " + add(10, 6)); Console.WriteLine("Subtracting two values: " + sub(10,4));Console.WriteLine("Subtracting two values: " + sub(10,4));
```

Pretty simple, huh? Happy coding!!

---

Original Source: https://www.mindstick.com/articles/951/delegate-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
