Delegate:-
A
delegate is an object that can refer to a method. Therefore, when you create a
delegate, you are creating an object that can hold a reference to a method.
Furthermore,
the method can be called through this reference. In other words, a delegate can
invoke the method to which it refers.
public delegate
int DelegatSample(int a,int b);
What
is the use of Delegates?
Suppose
if you have multiple methods with same signature (return type & number of parameters)
and want to call all the methods with single object then we can go for
delegates.
Delegates
are two types
- Single Cast Delegates
- Multi Cast Delegates
Single
Cast Delegates
Single
cast delegate means which hold address of single method like as explained in
above example.
Multicast
Delegates
Multi
cast delegate is used to hold address of multiple methods in single delegate.
To hold multiple addresses with delegate we will use overloaded += operator and
if you want remove addresses from delegate we need to use overloaded operator
-=
Multicast
delegates will work only for the methods which have return type only void. If
we want to create a multicast delegate with return type we will get the return
type of last method in the invocation list
Complete
Example of Single cast Delegates
public delegate
int DelegatSample(int a,int b);
public class
Sampleclass
{
public int
Add(int x, int
y)
{
return x + y;
}
public int
Sub(int x, int
y)
{
return x - y;
}
}
class Program
{
static void
Main(string[] args)
{
Sampleclass sc=new Sampleclass();
DelegatSample delgate1 = sc.Add;
int i = delgate1(10, 20);
Console.WriteLine(i);
DelegatSample delgate2 = sc.Sub;
int j = delgate2(20, 10);
Console.WriteLine(j);
}
}
Complete
Example of Multicast Delegates
public delegate
void MultiDelegate(int a,int b);
public class
Sampleclass
{
public static
void Add(int x,
int y)
{
Console.WriteLine("Addition Value: "+(x + y));
}
public static
void Sub(int x,
int y)
{
Console.WriteLine("Subtraction Value: " + (x - y));
}
public static
void Mul(int x,
int y)
{
Console.WriteLine("Multiply Value: " + (x * y));
}
}
class Program
{
static void
Main(string[] args)
{
Sampleclass sc=new Sampleclass();
MultiDelegate del = Sampleclass.Add;
del += Sampleclass.Sub;
del += Sampleclass.Mul;
del(10, 5);
Console.ReadLine();
}
}
6 important C# Delegate interview questions and answers
What is a delegate?
Delegate is a safe pointer to a method / function. So rather than pointing to the actual function you point to a delegate and via this delegate you can invoke the methods / functions.
What is a point of a pointer when we can invoke the actual function/method?
Many times rather than pointing to actual methods , we would like to point to abstraction. For example rather than pointing to “Add(int,int)” , “Substract(int,int)” , we create an abstract delegate pointer “Operation(int,int)” and point to any of them above.
What are the uses of delegates ?
There are two basic uses of delegates :-
- Callbacks / events.
- Method and function abstraction.
What are events ?
Events are encapsulation over delegates.
What are multi-cast delegates ?
If we want one delegate to point to multiple methods / function we use multicast delegates.
What are the different ways of creating a delegate in C# ?
- Action
- Func
- Predicate
- Lambda
- Anonymous types
No comments:
Post a Comment