2015-04-20 42 views
0

我正在使用此代码来构建我在每个点的3d表面图,但我有一个问题,我需要参数化我的函数,因此t变量将从0循环到T值,但我无法弄清楚,我该如何在代表内部做到这一点?c#lambda表达式与循环

编辑的第一块更加清晰:

/*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; 
+9

我认为你应该去一个混淆的C#代码比赛...这是三分钟我正在阅读第一个代码块,我仍然不确定谁是谁的参数 – xanatos

回答

0

如果您不需要表达式树,那么它应该是:

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; 
}; 

请注意,我必须更改t + deltaT加法器for

从那里你可以

new ILSurface(func); 

这是一个statement lambda,因为它使用了=>{ ... }代码。见https://msdn.microsoft.com/library/bb397687.aspxstatement lambdas