---
title: "Delegate in .net"  
description: "When we make an object of a class then we can access all function of a class and Base class functions too. Here we can see that a user can simple make"  
author: "AVADHESH PATEL"  
published: 2012-07-05  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/318/delegate-in-dot-net  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Delegate in .net

When we make an object of a class then we can [access](https://www.mindstick.com/articles/12994/how-foreigners-can-access-blocked-websites-in-china) all [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) of a class and [Base class](https://www.mindstick.com/blog/116/base-class-initilization) [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) too. Here we can see that a user can [simple](https://yourviews.mindstick.com/story/1469/5-simple-ways-to-stay-fit-amp-healthy) make an object of a class and access all information of class. But in real word this is drawback of an object. \

For example we are an [account](https://www.mindstick.com/articles/33699/how-tech-is-disrupting-the-accounting-world) holder of a bank, and we have [permission](https://www.mindstick.com/forum/159434/linux-service-permission-denied-error-adjust-permissions) to access all information, like other account holder’s details, bank deposited details and etc. think what [happens if](https://www.mindstick.com/forum/159800/what-happens-if-the-ldf-file-grows-too-large) bank provide all accessibly to account user…….

Through [delegate](https://www.mindstick.com/articles/777/delegate-and-event-in-c-sharp) we provide limited [accessibility](https://www.mindstick.com/blog/304975/why-image-alt-text-matters-for-seo-and-accessibility) to user.

##### Delegate in technical Word …

##### Delegate is a keyword that behaves runtime as a class.

##### Class’s object has reference of a class, but delegate’s object has reference

##### \
of a function.

##### \
Through delegate we can break accessibility of all function.

##### Through delegate we can save time.

##### Through delegate we can achieve dynamic method invocation.

##### \
Through delegate we called planed function.

##### \
It absolution support type softy.

##### \
Delegate is similar to function pointer in c.

##### \
Delegate’s object data type not fixed.

##### \
Through delegate object we can call any function from any class but both

##### data type and signature must be same.

##### \
Using delegate we make dynamic object.

##### \
Through delegate we achieve pure encapsulation.

##### \

Simple class provides us synchronous call back. That means a class

function can’t execute parallel. In other words, till one function execution

not completed then we can’t call another function but delegate achieve it

Asynchronous call back.

##### \
Delegate class is independent to any class means we can declare the

##### delegate either inside the class or outside the class.

##### Example

```
delegate int ptr();    class A     {            public int show()        {           return 12;        }    }    class B      {public int display()        {            return 15;        }    }    class Program    {        static void Main(string[] args)        {            A a = new A();            ptr k = new ptr(a.show);            Console.WriteLine(k());            Console.ReadLine();        }    }
```

##### Output

12

```
delegate int ptr();    class A    {        public int show()        {            return 12;        }    }   class B    {        public int display()        {            return 15;        }    }    class Program    {        static void Main(string[] args)        {            //A a = new A();            //ptr k = new ptr(a.show);            //Console.WriteLine(k());            B b = new B();            ptr k = new ptr(b.display);            Console.WriteLine(k());            Console.ReadLine();        }    }
```

##### Outout

15

---

Original Source: https://www.mindstick.com/blog/318/delegate-in-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
