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 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