How to call Math Operations using Delegates in C#?


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

Updated on: 22-Jun-2020

213 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements