Whenever
we want to create delegate methods we need to declare with delegate keyword and delegate methods signature should match
exactly with the methods which we are going to hold like same return types and
same parameters otherwise delegate functionality won’t work if signature not
match with methods.
Syntax of Delegate & Methods
Declaration
Check below sample
code for delegate declaration and methods declaration
public delegate int Delegatmethod(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;
}
}
|
If
you observe above code I declared Delegatmethod method with two parameters which
matching with methods declared in Sampleclass class.
Complete Example
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);
}
}
|
Output
Whenever
we run above code we will get output like as shown below
Add Result : 30
Sub Result : 10
|
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
Check below sample
code for delegate declaration and methods declaration
Syntax of Multicast Delegate & Method Declaration
Check below sample
code for multicast delegate declaration and methods declaration
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));
}
}
|
If
you observe above code I declared MultiDelegate method with void return type.
Complete Example
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();
}
}
|
Output
Whenever
we run above code we will get output like as shown below
Addition Value : 15
Subtraction Value : 5
Multiply Value : 50
No comments:
Post a Comment