Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 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 delegates
delegate void Myrect(double len, double width);
  
class GFG{
  
// Finding Area of rectangle
static void Area(double len, double width)
{
    double res1 = len * width;
    Console.WriteLine("Area:" + res1);
}
  
// Finding perimeter of rectangle
static void Perimeter(double len, double width)
{
    double res2 = 2 * (len + width);
    Console.WriteLine("Perimeter:" + res2);
}
  
// Finding diagonal of rectangle
static void Diagonal(double len, double width)
{
    double res3 = Math.Sqrt(len * len + width * width);
    Console.WriteLine("Diagonal:" + res3);
}
  
// Driver code
static 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).


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!