---
title: "Delegates in C#"  
description: "Delegates is referred to as a type safe function pointer in C#. Which holds the reference of method with a particular argument list and return type."  
author: "Anupam Mishra"  
published: 2015-12-19  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/11961/delegates-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Delegates in C#

Delegates is [referred to as](https://answers.mindstick.com/qa/97098/which-layers-are-referred-to-as-network-support-layers) a type safe function [pointer in C](https://answers.mindstick.com/qa/104680/what-is-a-pointer-in-c)#. Which holds the [reference](https://www.mindstick.com/forum/34619/call-by-value-and-call-by-reference) of method with a particular argument list and return type. When you want to initiate a delegate in a program you can associated its instance with any method that are matches its [signature](https://www.mindstick.com/interview/516/explain-about-xml-signature) and return type. You can call or invoke the method through the delegate instance. The syntax of the delegates are as follows:\

```
<access specifier> delegate<return type> <DeligateName> (<data type> <argument list>);
```

When we initiate the delegate just same like as creating class instances such as follows:\

```
<DeligateName> <InstanceName>=new <DeligateName>(<Methodname>);
```

If we are using **[static methods](https://www.mindstick.com/forum/161339/static-methods-in-python)** in a class then no need to create instance of class it is call to delegate instance. If you are define non [static method](https://www.mindstick.com/forum/216/can-i-override-a-static-method) in a class that are access via class_object.MethodName pass [as an argument](https://answers.mindstick.com/qa/31668/in-c-if-you-pass-an-array-as-an-argument-to-a-function-what-actually-gets-passed) in creation time of delegate.

You are also holds multiple methods in a delegate but every method return **void** else there is a run-time exception.

Its major role in event design that encapsulate the method call from caller. Caller has no need to access other [properties](https://yourviews.mindstick.com/view/81845/now-it-s-possible-to-predict-properties-of-any-molecule), method on the object of a class.

For example, we create a delegate named 'DeligateDemo' that are taking two [arguments](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments) and we are create another delegate that are represented as a multi delegate and no argument list.

```
using System;namespace DelegateEx{    //creating delegate named 'DelegateDemo'    public delegate int DelegateDemo(int Num1, int Num2);    public delegate void MulDelegate();    class Program    {        //Creating non static method       public int Calculate(int Number1, int Number2)        {            int res = Number1 * Number2;            return res;        }       public static void Display()       {           Console.WriteLine("You are in Display Function");       }       public void Show()       {           Console.WriteLine("You are in show Function");       }        static void Main(string[] args)        {            //creating instance of Program class named 'p'.            Program p = new Program();            //Input two integer parameter            Console.WriteLine("Enter the First value: ");            int num1 = Convert.ToInt32(Console.ReadLine());            Console.WriteLine("Enter the Second value: ");            int num2 = Convert.ToInt32(Console.ReadLine());            //Creating instance of delegate 'DelegateDemo'            DelegateDemo obj = new DelegateDemo(p.Calculate);            //passing values to the deligate            int Result=obj(num1, num2);            //Printing results            Console.WriteLine("Multiplication :"+Result);            //Creating multi Delegate  instance  & accesing static method            MulDelegate obj1 = new MulDelegate(Display);            //Calling Display via delegate instance            obj1();            //Non-static function via class object in the muldelegate            obj1 = new MulDelegate(p.Show);            obj1();            Console.ReadKey();        }    }}
```

**Output:**

![Delegates in C#](https://www.mindstick.com/mindstickarticle/f14a265a-fabd-47e9-add8-beff37663bda/images/e34d6bf6-b7f8-49cd-bd74-0c51fc12bf3a.png)\

---

Original Source: https://www.mindstick.com/articles/11961/delegates-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
