How to call Math Operations using Delegates in C#?

CsharpProgrammingServer Side Programming

To understand how to call Math Operations using Delegates in C#, let us see an example wherein we will divide a number.

We have a class and a function in it −

public class Demo {
   public static double DivideFunc(double value) {
      return value / 5;

Now, our delegate −

delegate double myDelegate(double x);

Set a value and call −

myDelegate[] val = { Demo.DivideFunc };

result(val[0], 20);

Math operation is called using delegate −

static void result(myDelegate d, double value) {
   double result = d(value);
   Console.WriteLine("Result = {0}", result);

The above displays the following result for “value/ 5” i.e. 20/5 −

Result = 4
Published on 20-Aug-2018 08:49:01