Home  >  Article  >  Backend Development  >  How to call mathematical operations using delegates in C#?

How to call mathematical operations using delegates in C#?

WBOY
WBOYforward
2023-08-26 19:57:14464browse

如何在 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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete