---
title: "Delegate"  
description: "Delegates in C# allow us to dynamically change the reference to the methods in a class. A delegate is a reference type variable, which holds the refer"  
author: "Anonymous User"  
published: 2012-09-11  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/362/delegate  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Delegate

[Delegates in C#](https://www.mindstick.com/forum/33765/delegates-in-c-sharp) allow us to dynamically change the reference to the methods in a class. A [delegate](https://www.mindstick.com/articles/777/delegate-and-event-in-c-sharp) is a [reference type](https://www.mindstick.com/interview/235/what-is-difference-between-value-types-and-reference-types-in-vb-dot-net-or-c-sharp-value-types-vs-reference-types) [variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation), which holds the reference to a method. This [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) can be changed at runtime as desired. This means that you can decide the [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) of a method a runtime, based on the [requirements](https://www.mindstick.com/articles/12850/drivers-license-requirements) of [your application](https://www.mindstick.com/forum/34702/please-tell-me-what-is-cross-site-scripting-and-how-is-it-harmful-for-your-application).

Declaring Delegates

```
public delegate int MyDelegate(int num1, int num2);    // this declaration defines a delegate named MyDelegate,     //which will encapsulate any method that takes two integer parameters and      returnsinteger value.
```

##### Instantiating Delegates

```
MyDelegate objMyDelegate = new MyDelegate(SampleDelegate.Sum);
```

##### Example: -

## ```
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Delegates{public delegate int MyDelegate(int num1, int num2);// this declaration defines a delegate named MyDelegate, //which will encapsulate any method that takes two integer parameters and returnsinteger value.class SampleDelegate{publicstatic int Sum(int var1, int var2){return var1 + var2;}publicstatic void Subtract(int var1,int var2){Console.WriteLine(var1 - var2);}}class Program{staticvoid Main(string[] args){//Creating the Delegate InstanceMyDelegate objMyDelegate = new MyDelegate(SampleDelegate.Sum);Console.WriteLine("Sum of two Numbers is : "+objMyDelegate(2, 3)); //use a delegate for processingMyDelegate objMyDelegate1 = new MyDelegate(SampleDelegate.Subtract);// It will generate n error because function definition is not mathing// with the delegate its has been calling}}}
```

##### Output

[Sum of two](https://www.mindstick.com/forum/159292/write-a-node-js-function-to-find-the-sum-of-two-numbers-passed-as-arguments) Numbers is: 5

---

Original Source: https://www.mindstick.com/blog/362/delegate

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
