C# Program to Demonstrate the Example of an Array of Delegates - GeeksforGeeks

04 Jan, 2022
In C#, a delegate is an object which refers to a method or it is a reference type variable that can hold a reference to the methods. Delegates are similar to the C/C++ function pointer. It also provides a way that tells which method is to be called when an event is triggered. As the name suggests an array of delegates means declaring an array of delegates in the array using the following syntax:
Syntax:
name[] object = new name[size];
Where name is the delegate array and size is the delegate array size. We can also pass the methods to a delegate array through its index.
name[0] = method1;
name[1] = method2;
-----------------
-----------------
name[n] = methodn;
We can call the delegate by using its name.
delegate_array();
Now we will create three methods and create an array of delegates to point the methods and we call methods using an array of delegates.
Approach:
1. Declare the delegate here Myrect() is the delegate.
delegate void Myrect(double len, double width);
2. Create three normal methods named Area, Perimeter, and Diagonal.
3. Create an array of delegates of size 3.
Myrect[] del = new Myrect[3];
4. Pass these methods to each array index of the delegate.
del[0] = Area;
del[1] = Perimeter;
del[2] = Diagonal;
5. Call the delegate iterating the array inside for loop.
for(int i = 0; i < 3; i++)
{
    del[i](len, width);
}
Example:
C#
// C# program to illustrate how to create// an array of delegates.using System;  // Creating delegatesdelegate void Myrect(double len, double width);  class GFG{  // Finding Area of rectanglestatic void Area(double len, double width){    double res1 = len * width;    Console.WriteLine("Area:" + res1);}  // Finding perimeter of rectanglestatic void Perimeter(double len, double width){    double res2 = 2 * (len + width);    Console.WriteLine("Perimeter:" + res2);}  // Finding diagonal of rectanglestatic void Diagonal(double len, double width){    double res3 = Math.Sqrt(len * len + width * width);    Console.WriteLine("Diagonal:" + res3);}  // Driver codestatic void Main(){    double len = 4, width = 5;          // Create an array of delegates    // with array size 3    Myrect[] del = new Myrect[3];      // Pass the methods to array index    del[0] = Area;    del[1] = Perimeter;    del[2] = Diagonal;      Console.WriteLine("Data:");    for(int i = 0; i < 3; i++)     {        del[i](len, width);    }}}
Output:
Data:
Area:20
Perimeter:18
Diagonal:6.40312423743285
Explanation: In the above example, we create a delegate named “Myrect(double len, double width)” and it takes two parameters. Now we create a class named “GFG” that contains three methods named Area, Perimeter, and Diagonal, and these methods take two arguments named len(i.e., length of the rectangle) and width(i.e., width of the rectangle). In the main method, we create two variables and initialize them with the length and width of the rectangle(i.e., 4, and 5). Now we create an array of delegates of size 3 named “del” and pass these methods to each array index of the delegate. i.e., del[0] = Area; del[1] = Perimeter; del[2] = Diagonal;. Display the data by calling the delegate(i.e, del[i](len, width)) iterating the array inside for loop(Here the for loop iterate three times).
as5853535
CSharp-Delegates
Picked
C#
C# Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
C# | Inheritance
Convert String to Character Array in C#
Socket Programming in C#
Program to Print a New Line in C#
Getting a Month Name Using Month Number in C#
Program to find absolute value of a given number