14

I am new to C#, but from my understanding this code should work. Why doesn't it work?

This is an example of my code.

List<Car> cars // This has many cars initialized in it already
if (() => {
   foreach(Car car in cars){
       if (car.door == null) return true;
   }
}){then .......}

Simply put, all I want the code to do is run the if statement if any car does not have a door.

After trying to compile I get this error:

Cannot convert lambda expression to type 'bool' because it is not a delegate type.

1
  • You're not even using the correct lambda syntax. It would be () => {..., not ()={... Of course, it wouldn't work if you were using it, but that's another story. Commented Jul 7, 2014 at 17:00

3 Answers 3

35

If you want to check if any car does not have a door then simply use Enumerable.Any - it determines whether any element of a sequence satisfies a condition:

if (cars.Any(c => c.door == null))
   // then ...

Just for fun: you should execute lambda to get boolean result in if condition (but for this case use Any)

Func<bool> anyCarDoesNotHaveDoor = () => { 
    foreach(var car in cars)
       if (car.door == null)
           return true;
    return false; 
};

if (anyCarDoesNotHaveDoor())
   // then ...

I introduced local variable to make things more clear. But of course you can make this puzzle more complicated

 if (new Func<bool>(() => { 
        foreach(var car in cars)
           if (car.door == null)
               return true;
        return false; })())
    // then ...    
Sign up to request clarification or add additional context in comments.

3 Comments

Just to make sure could you have done this. if (Func<bool>() => {})
@user3813249, You still have to call the delegate. A delegate in and of itself makes no sense to an if statement.
@user3813249 Func<bool> delegate should return boolean value, so you can't just assign empty body to it, like you can do with Action
9

Well, the error says it all. An if statement is expecting a boolean expression which a delegate is not. If you were to call the delegate (assuming it returned a bool), you would be fine. However, if does not know to call it.

The easy way to do this is with the Any LINQ extension method:

if (cars.Any(car => car.door == null))

The Any method knows to actually invoke the lambda expression on each member of the collection, and returns a bool. This makes it a valid boolean expression for the if statement.

2 Comments

This seems a misleading explanation. A delegate is an expression, as evidenced by your calling it a lambda expression a moment later; it's just that if demands its condition be a Boolean expression - one of type bool. Lambdas are of type Func<T> for some return type T and hence can't satisfy that requirement. (They can also be expression-tree types, depending on inference from the context of their use, but they can't be bool.)
@00Davo You are correct, that could have been worded better. I'll edit and see if I can clear it up.
2

In case you want to actually do something to cars without doors:

foreach (var car in cars.Where(car => car.door == null)) {
    car.door = <whatever>;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.