C# Program to Call Math Operations using Delegates

This is a C# Program to call math operations using delegates.

Problem Description

This C# Program Calls Math Operations using Delegates.

Problem Solution

Here the Delegates are used to encapsulate functions into callable function objects that are used much as function pointers are used in C, C++, and other languages. Like function pointers, delegates enable you to separate a function reference from its implementation, allowing the implementation to exist in a separate module.

Program/Source Code

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

/*
 *  C# Program to Call Math Operations using Delegates
 */
 
using System;
public class MathOperations
{
    public static double Multiply(double value)
    {
        return value * 2;
    }
 
    public static double Square(double value)
    {
        return value * value;
    }
}
 
 
delegate double DoubleOp(double x);
 
class Application
{
    static void Main()
    {
        DoubleOp[] operations =
            {
               MathOperations.Multiply,
               MathOperations.Square
            };
 
        for (int i = 0; i < operations.Length; i++)
        {
            Console.WriteLine("Operation[{0}]:", i);
            ProcessAndDisplayNumber(operations[i], 5.0);
            ProcessAndDisplayNumber(operations[i], 13.55);
            ProcessAndDisplayNumber(operations[i], 1.732);
            Console.WriteLine();
        }
        Console.ReadLine();
    }
 
    static void ProcessAndDisplayNumber(DoubleOp action, double value)
    {
        double result = action(value);
        Console.WriteLine(
           "Value : {0}  Result : {1}", value, result);
    }
}
Program Explanation

This C# program is used to call math operations using delegates. A delegate is a reference type variable that holds the reference to a method. The reference can be changed at runtime. Using for loop perform different functions by calling the function using delegate object variable.

advertisement
advertisement

Here the Delegates are used to encapsulate functions into callable function objects that are used much as function pointers. Like Function pointers, delegates enable to separate a function reference from its implementation, allowing the implementation to exist in a separate module.

Runtime Test Cases
 
Operation[0]:
Value : 5  Result : 10
Value : 13.55  Result : 27.1
Value : 1.732  Result : 3.464
 
Operation[1]:
Value : 5  Result : 25
Value : 13.55  Result : 183.6025
Value : 1.732  Result : 2.999824

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.