简体   繁体   中英

c# lambda expression with for loop

I'm using this code to build my 3d surface plot in each point, but I have a problem that I need to parametrize my function so t variable will be looped from 0 to T value, but I can't figure it out how can I do it inside the delegate?

edited the first block for more clarity:

/*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))
}

Resulting pseudocode is something like:

float functionValue = 0;
for (double t = 0; t < T; t + deltaT)
{
     /*t is loop parameter here*/
     functionValue += (float) (1/(x+y+t));   
}
return functionValue;

If you don't need an Expression Tree, then it should be:

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

Note that I had to change the t + deltaT adder of the for

From there you can

new ILSurface(func);

This is a statement lambda , because it uses { ... } code after the => . See https://msdn.microsoft.com/library/bb397687.aspx statement lambdas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM