I cannot return from a ForEach, here is my code:
cards.ForEach(delegate(Card c){
if (c.GetFaceValue().ToString() == card) {
return "Your Card has been located";
}
error:
Cannot convert anonymous method to delegate type ‘… .Card>’ because some of the return types in the block are not implicitly convertible to the delegate return type
the following wont work either, as CheckCard can only return void:
cards.ForEach(CheckCard);
public string CheckCard(Card c) {
if (c.GetFaceValue().ToString() == card) { // card = global
return "Your Card has been located";
}
}
error:
‘string SharedGamesClasses.Hand.CheckCard(SharedGamesClasses.Card)’ has the wrong return type
You’re currently trying to return a message from the anonymous function and not your calling code; that won’t work as the anonymous function is an
Action<T>, which can’t return anything.Do you just want to see if any element in
cardsmatchescardand return the message if so?Use
.Any()instead of.ForEach():