How to call mathematical operations using delegates in C#?

WBOY
Release: 2023-08-26 19:57:14
forward
483 people have browsed it

如何在 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;
   }
}
Copy after login

Now, our delegate −

delegate double myDelegate(double x);
Copy after login

sets a value and calls −

myDelegate[] val = { Demo.DivideFunc };

result(val[0], 20);
Copy after login

Math operation is called using delegate −

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

The above code shows the result for "value/ 5", which is 20/5 -

Result = 4
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!