C# Program to Illustrate Array of Delegates

This is a C# Program to illustrate array of delegates.

Problem Description

This C# Program Illustrates Array of Delegates.

Problem Solution

Here an array of delegate is created similar to that of normal declaration of the delegate.

Program/Source Code

Here is source code of the C# Program to Illustrate Array of Delegates. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Illustrate Array of Delegates
 */
using System;
delegate double Measure(double R);
public class Circle
{
    const double PI = 3.14159;
    public double Diameter(double Radius)
    {
        return Radius * 2;
    }
 
    public double Circumference(double Radius)
    {
        return Diameter(Radius) * PI;
    }
 
    public double Area(double Radius)
    {
        return Radius * Radius * PI;
    }
}
public static class Program
{
    static int Main()
    {
        double R = 10;
        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("Diameter:      {0}", D);
        Console.WriteLine("Circumference: {0}", C);
        Console.WriteLine("Area:          {0}\n", A);
        Console.ReadLine();
        return 0;
    }
}
Program Explanation

This C# program is used to illustrate array of delegates. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. Create an object ‘circ’ variable for circle class.

advertisement
advertisement

The measure[] variable is an array delegate. An array of a delegate is created similar to that of normal declaration of the delegate. Using measure[] array compute the Diameter, Circumference, and Area procedure and print the values.

Runtime Test Cases
 
Diameter         : 20  
Circumference    : 62.8318
Area             : 314.159

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.