Chapter: C# and .NET Framework

C# Delegates

Syntax of Delegate & Methods Declaration

DELEGATES

 

Ø   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 

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

 

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

 

Example of Multicast Delegate & Method 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));

}

}

 

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

Addition Value : 15

Subtraction Value : 5

Multiply Value : 50

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
C# and .NET Framework : C# Delegates |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.