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 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 holder of a bank, and we have permission to access all information, like other account holder’s details, bank deposited details and etc. think what happens if bank provide all accessibly to account user…….
Through delegate we provide limited 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 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
22
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
Leave Comment