我可以将委托定义为c#中函数的限制吗?
本文关键字:函数 定义 我可以 | 更新日期: 2023-09-27 18:03:36
假设我有两个函数f(x,y)和g(x)。在数学中,如果y是固定的,比如y_fix,可以定义:
g(x) := f(x,y_fix)
现在假设g()是c#委托,f是一个已实现的函数。我可以用类似的方式定义g(x)吗?例如
public delegate double SingleVariableFunction(double x);
public class SomeClass
{
SingleVariableFunction g;
double y_fix;
public SomeClass()
{
y_fix = 2;
g = f( ,y_fix); /// Is this possible somehow?
}
public double f(double x, double y)
{
return x + y;
}
}
这叫做局部应用。
在c#中,你可以用lambda表达式构造g
:
g = x => f(x, y_fix);