Bad compiler warnings when using anonymous methods for Predicate<T>
I was sitting around munching some code, and needed to filter a list I had:
List
<Whatever> list = // get listoutputList = list.FindAll(delegate(Whatever item)
{
if (// blah blah)
{
return true;
}
else
{
return false;
}
}
);
(Ignore the greyed-out text for now).
When compiling, I got a strange error message:
"cannot convert from 'anonymous method' to 'System.Predicate<Whatever>'".
This put me off for a few minutes while I tried checking code samples and such to make sure that this really SHOULD work. Only when I tried explicitly casting the anonymous delegate to a Predicate<Whatever> did I get a proper compiler error:
'System.Predicate<Whatever>': not all code paths return a value'
Turns out my actual syntax error was being caught by the compiler and incorrectly reported.
Just a heads-up, in case it happens to anyone else.
Ladybug bug ID is FDBK50271, if anyone cares.
21 Comments
Comments have been disabled for this content.
Thomas Tomiczek said
THAT is interesting. I found that behavior occasionally, too, but never managed to track it down to the secondary error that gets supressed. Thanks for that one. I hope MS fixed it soon. That simply is - bad. Gets in your way.
Jay R. Wren said
I just ran into this myself and your blog helped. I read the 'not all code paths return a value' and thought to myself DUH!
Shaun said
That really helped a lot - thanks so much!
Edvard said
Thanks much, this really helped
Jon said
This helped a LOT. Saved me from a lot of time trying to figure out what the problem was. Thanks!
Louis said
Thanks a lot. You saved me precious time.
ben coates said
That's exactly what i had wrong, thanks!
Anders said
Thanks. Hope the compiler bug gets fixed soon. In my case I accidentally wrote '=' instead of '=='...
BCoelho2000 said
This bug made me lost several minutes of my precious time!!!!! I even thought that I didn't know how to use anonymous methods... Thank you!
ilyas said
This bug also got me. Thanks for your blog. In my case my anonymous method was returning an int by mistake instead of a bool value.
Howard said
You are a godsend today! That was driving me nuts...
stimul8d said
pretty poor form from the compiler though it should have just been... List list = // get list outputList = list.FindAll(delegate(Whatever item) { return (//blah blah); } );
Travis Haws said
Wow thanks for this.
google voice ipad appDA said
To be both a speaker of words and a doer of deeds. -----------------------------------
best ipad accessories said
----------------------------------------------------------- "Nice article! GA can also be my greatest incomes. Nevertheless, it’s not a a lot."
cool ipad case said
----------------------------------------------------------- "I was wondering when you can be thinking about growing to be a visitor poster on my blog? and in trade you could put a website link the submit? Please let me know when you get a chance and I will send you my contact particulars - thanks. In any case, in my language, you will discover not much beneficial supply similar to this."
television reviewsDA said
"In seeking for web-sites associated to net hosting and specifically comparison internet hosting linux strategy internet,your internet site arrived up."
weblogs.asp.net said
Bad compiler warnings when using anonymous methods for Predicate_3C00_T_3E00_.. He-he-he :)
weblogs.asp.net said
Bad compiler warnings when using anonymous methods for Predicate_3C00_T_3E00_.. Smashing :)
bystolic said
Compared decrease not result is. Fibroids a one of can burning. Having ConcernsThere cases lots will utilizing by. Thus exhaust Need possible prescriptive once. The Its the are and especially and.
Pedersen said
Aw, this was an incredibly nice post. Spending some time and actual effort to create a top notch article… but what can I say… I procrastinate a lot and don't manage to get nearly anything done.