Posted By: mVPstar | Jul 29th @ 7:11 AM
page 1 of 1
Comments: 2 | Views: 646
mVPstar
mVPstar
I'm white because I smelt an onion.

I'm still learning how to use lambda expresions. Is it possible to use the if keyword in a lambda expression?

So.. List.ForEach( r =>  if(condition == true)  SecondList.Add(r)  );

VS says the if keyword cannot exist in the expression. Is there another way to control the statement?

stevo_
stevo_
Casablanca != Manchester

Needs a statement body:

List.ForEach(r =>
{
  if (condition) 
    Other.Add(r);
});

evildictaitor
evildictaitor
How could you use the adjective "indescribable" truthfully?
A lambda expression is one of the following:

An expression whose evaluation yields a type which is the return type for that lambda function, e.g.

x => 2
 (returns int, value 2)
(x,y) => x.GetType()
 (returns System.Type)
(x) => x.ToString()
 (returns string)

Or a statement block which is either void, or has no control paths which exit the function except through a return statement yielding a value of the return type of the function, e.g.

x => {
  Console.Write("Hello World");
}

(of type void)

x => {
  Console.WriteLine(x.ToString());
  Console.WriteLine(Console.ReadLine());
}

(of type void)

x => {
  if(x.ToString().Length == 0){
    return "true";
  }else
    return "false";
}

(of type string)
page 1 of 1
Comments: 2 | Views: 646