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
Example:
public delegate int DelegatSample(int a,int b);
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);
}
}
No comments:
Post a Comment