Home > Article > Backend Development > How to call mathematical operations using delegates in C#?
To understand how to use delegates to call mathematical operations in C#, let's look at an example in which we will divide a number.
We have a class and a function:
public class Demo { public static double DivideFunc(double value) { return value / 5; } }
Now, our delegate −
delegate double myDelegate(double x);
sets a value and calls −
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 code shows the result for "value/ 5", which is 20/5 -
Result = 4
The above is the detailed content of How to call mathematical operations using delegates in C#?. For more information, please follow other related articles on the PHP Chinese website!