---
title: "Delegate"  
description: "In this blog, I’m explaining the concept of delegate.  A delegate in C# is similar to a function pointer in C or C++. In C#, a delegate is type safe o"  
author: "Anchal Kesharwani"  
published: 2014-08-12  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/675/delegate  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Delegate

In this blog, I’m explaining the concept of delegate.\

A [delegate in C#](https://www.mindstick.com/forum/33917/how-to-callback-operation-using-delegate-in-c-sharp) is similar to a function [pointer in C](https://answers.mindstick.com/qa/104680/what-is-a-pointer-in-c) or C++. In C#, a delegate is type safe object that can point to another method (or multiple methods) that means a delegate can point to a method, which is having same [signature](https://www.mindstick.com/interview/516/explain-about-xml-signature) as that of the delegate in the [application](https://www.mindstick.com/articles/12824/calculator-application-in-android), which can invoked at later time.

##### There are three steps in defining and using delegates:

1. Declaration

2. Instantiation

3. Invocation

##### Declaring

delegate <return type><delegate-name><[parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) list>

Consider the following example:

public delegate void DelegateExample (string s)

##### Example

```
using System;namespace DelegateExample{    // Declaration of delegate    public delegate void DelegateExample();    class Test    {        public static void MyMessage() // create a method for delegate        {            Console.WriteLine("I was called by delegate ...");        }        public static void Main()        {            Test t = new Test();            // Instantiation of delegate            DelegateExample simpleDelegate = new DelegateExample(MyMessage); // It the function name            // Invocation of delegate            simpleDelegate(); // call this method             Console.ReadKey();        }    }}
```

Output is: I was called by delegate ...

In this example, how to a delegate declare, instantiate, and how to call.

##### Multicast Delegate

[Multicast delegate](https://www.mindstick.com/interview/1073/what-s-a-multicast-delegate) means that they can point to more than one function at a time. Delegates objects can be composed using the “+” operator. Only delegates of the same type can be composed. The "-" operator can be used to remove a [component](https://www.mindstick.com/articles/12262/learn-how-to-make-a-component-in-magento-2) delegate from a composed delegate.

##### Example

```
using System;delegate void MulticastDelegateExample(int x, int y); //declare a multicast delegatenamespace MulticastDelegate{      class TestDelegate   {       public static void AddNum(int p,int q)       {           Console.WriteLine("Add two numbers :" + (p + q));       }       public static void MultNum(int p, int q)       {           Console.WriteLine("Multiply two numbers :" + (p + q));       }       static void Main(string[] args)       {           //create delegate instances           MulticastDelegateExample nc;           MulticastDelegateExample nc1 = new MulticastDelegateExample(AddNum);           MulticastDelegateExample nc2 = new MulticastDelegateExample(MultNum);           nc = nc1;           nc += nc2;           //calling multicast both method together           nc(1,2);           nc -= new MulticastDelegateExample(AddNum);           nc(2, 3); //calling only second method MultNum                     Console.ReadKey();       }   }  }
```

In this example, there are two methods and create two [instances](https://answers.mindstick.com/qa/92508/how-can-you-hide-the-sql-server-instances) and one

\

[reference](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) of MulticastDelegateExample. Reference assign both instance

\

and call both method together and after only call second method.

---

Original Source: https://www.mindstick.com/blog/675/delegate

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
