[英]C# Func<> - looking for an explanation - not a solution
i am using Math.net, among other methods i use integration, the integration function i use is defined as foloows: 我正在使用Math.net,我使用集成的其他方法,我使用的集成函数定义为foloows:
public static double IntegrateComposite(
Func<double, double> f,
double intervalBegin,
double intervalEnd,
int numberOfPartitions)
yet, my call syntax is: 然而,我的调用语法是:
p = IntegrateComposite(
tau => MyFunction(r, tau, appCfg, Ta, Da) * ProbDensity(tau),
lowLim, hiLim, 32)
My function is better defined as Func<double, double, double, double, double, double>
and not the above Func<double, double>
, still everything works fine Why? 我的函数更好地定义为Func<double, double, double, double, double, double>
而不是上面的Func<double, double>
,仍然一切正常为什么?
Func<double, double>
means Func<double, double>
表示
a function which takes 1 argument of type double in, and returns double 取一个double类型参数的函数,返回double
That's exactly what your arrow function does, no matter how many external variables are involved into calculation. 这正是你的箭头函数所做的,无论计算中涉及多少外部变量。
Func<T1, T2, T3..., T(n), TResult>
is a type that represents a method which takes (n) number of parameters with specified types (could be 0), and returns an object of type TResult
. Func<T1, T2, T3..., T(n), TResult>
是一种类型,表示采用(n)具有指定类型的参数(可以是0)的方法,并返回TResult
类型的对象。
By Func<double, double>
, it specifies that it must be a method which takes one double
as a parameter and return a double
. 通过Func<double, double>
,它指定它必须是一个方法,它将一个double
作为参数并返回一个double
。
In this example, Lambda function is used (which also can be represented by Func). 在此示例中,使用Lambda函数(也可以由Func表示)。
tau => {return MyFunction(r, tau, appCfg, Ta, Da) * ProbDensity(tau)},
The only parameter you pass into the lambda function is tau
, which can be seen on the left hand side of the arrow. 传递给lambda函数的唯一参数是tau
,可以在箭头的左侧看到。 Other variables such as r
, tau
, appCfg
... are captured variables. 其他变量如r
, tau
, appCfg
...是捕获的变量。
Another example of generic delegates: (obj, e) => {Console.WriteLine(obj)}
is passed two variables named obj
and e
. 泛型委托的另一个例子: (obj, e) => {Console.WriteLine(obj)}
传递两个名为obj
和e
变量。 Unlike your example, It returns nothing. 与您的示例不同,它什么都不返回。 This is represented as Action<T1, T2>
which represents a method that returns nothing. 这表示为Action<T1, T2>
,表示不返回任何内容的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.