Arrays and Delegates

 Introduction
 To further refine the call to a group of methods that perform the same kind of task, you can declare an array of delegates. First, create the necessary delegate. Here is an example: using System; delegate double Measure(double R); public static class Program { static int Main(string[] args) { return 0; } }

Before creating the array, you must first know or have the methods you would be referring to. These methods must have a similar signature. This means that they must return the same type of value, they must have the same number of arguments and they must  have the  same type(s) of argument(s), if any. Here are examples of such functions:

using System;

delegate double Measure(double R);

public class Circle
{
const double PI = 3.14159;

{
}

{
}

{
}
}

public static class Program
{
static int Main()
{
return 0;
}
}
 An Array of Delegates

To create an array of delegates, declare a normal array as we have done so far. You can initialize each member using its index and calling the corresponding method. This can be done as follows:

using System;

delegate double Measure(double R);

public class Circle
{
const double PI = 3.14159;

{
}

{
}

{
}
}

public static class Program
{
static int Main()
{
double R = 12.55;
Circle circ = new Circle();
Measure[] Calc = new Measure[3];

Calc[0] = new Measure(circ.Diameter);
double D = Calc[0](R);
Calc[1] = new Measure(circ.Circumference);
double C = Calc[1](R);
Calc[2] = new Measure(circ.Area);
double A = Calc[2](R);

Console.WriteLine("Circle Characteristics");
Console.WriteLine("Diameter:      {0}", D);
Console.WriteLine("Circumference: {0}", C);
Console.WriteLine("Area:          {0}\n", A);

return 0;
}
}

This would produce:

Circle Characteristics
Diameter:      25.1
Circumference: 78.8539
Area:          494.808
 Practical Learning: Using an Array of Delegates
1. Start Microsoft Visual C# Express Edition and create a new Console Application named MultipleChoiceQuestion1
2. Change the Program.cs file as follows: