2015-04-20 44 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