[英]c# lambda expression with for loop
我正在使用此代码在每个点上构建3d表面图,但是我有一个问题,我需要对函数进行参数化,以便t变量将从0循环到T值,但是我无法弄清楚如何我在代表内部做吗?
编辑了第一个块以更加清楚:
/*this is code for building 3d surface plot, parameter delegate is counting Z
value in each (x, y) point.
x, y are axis variables. t is constant here*/
new ILPlotCube()
{
new ILSurface((x, y) => (float) (1/(x+y+t))
}
产生的伪代码类似于:
float functionValue = 0;
for (double t = 0; t < T; t + deltaT)
{
/*t is loop parameter here*/
functionValue += (float) (1/(x+y+t));
}
return functionValue;
如果不需要表达式树,则应为:
Func<float, float, float> func = (x, y) =>
{
float functionValue = 0;
for (double t = 0; t < T; t += deltaT)
{
/*t is loop parameter here*/
functionValue += (float)(1 / (x + y + t));
}
return functionValue;
};
请注意,我必须更改for
的t + deltaT
加法器
从那里你可以
new ILSurface(func);
这是一个statement lambda
,因为它在=>
之后使用{ ... }
代码。 请参阅https://msdn.microsoft.com/library/bb397687.aspx statement lambdas
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.