Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations
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 list

outputList = 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.

Published Monday, May 22, 2006 9:33 PM by AvnerK

Filed under: ,

Comments

# re: Bad compiler warnings when using anonymous methods for Predicate<T>@ Tuesday, May 23, 2006 6:17 AM

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.

Thomas Tomiczek

# re: Bad compiler warnings when using anonymous methods for Predicate<T>@ Wednesday, May 31, 2006 11:50 PM

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!

Jay R. Wren

# re: Bad compiler warnings when using anonymous methods for Predicate<T>@ Tuesday, August 29, 2006 11:47 AM

The id for the bug is now at this link:

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=91434

Josh

# re: Bad compiler warnings when using anonymous methods for Predicate<T>@ Friday, September 28, 2007 12:24 PM

That really helped a lot - thanks so much!

Shaun

# re: Bad compiler warnings when using anonymous methods for Predicate<T>@ Wednesday, November 07, 2007 10:49 AM

Thanks much, this really helped

Edvard

# re: Bad compiler warnings when using anonymous methods for Predicate<T>@ Friday, November 16, 2007 12:20 PM

This helped a LOT. Saved me from a lot of time trying to figure out what the problem was. Thanks!

Jon

# re: Bad compiler warnings when using anonymous methods for Predicate<T>@ Thursday, March 27, 2008 2:30 PM

Thanks a lot. You saved me precious time.

Louis

# re: Bad compiler warnings when using anonymous methods for Predicate<T>@ Tuesday, April 08, 2008 8:14 PM

That's exactly what i had wrong, thanks!

ben coates

# re: Bad compiler warnings when using anonymous methods for Predicate<T>@ Saturday, May 24, 2008 9:53 AM

Thanks. Hope the compiler bug gets fixed soon. In my case I accidentally wrote '=' instead of '=='...

Anders

# re: Bad compiler warnings when using anonymous methods for Predicate<T>@ Monday, October 27, 2008 10:19 AM

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!

BCoelho2000

# re: Bad compiler warnings when using anonymous methods for Predicate<T>@ Thursday, February 05, 2009 3:08 PM

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.

ilyas

Leave a Comment

(required) 
(required) 
(optional)
(required)