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.

Posted: May 22 2006, 09:33 PM by AvnerK | with 25 comment(s)
Filed under: ,

Comments

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.
# May 23, 2006 6:17 AM

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!
# May 31, 2006 11:50 PM

Josh said:

# August 29, 2006 11:47 AM

Shaun said:

That really helped a lot - thanks so much!

# September 28, 2007 12:24 PM

Edvard said:

Thanks much, this really helped

# November 7, 2007 10:49 AM

Jon said:

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

# November 16, 2007 12:20 PM

Louis said:

Thanks a lot. You saved me precious time.

# March 27, 2008 2:30 PM

ben coates said:

That's exactly what i had wrong, thanks!

# April 8, 2008 8:14 PM

Anders said:

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

# May 24, 2008 9:53 AM

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!

# October 27, 2008 10:19 AM

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.

# February 5, 2009 3:08 PM

Howard said:

You are a godsend today! That was driving me nuts...

# August 13, 2009 2:38 PM

akh said:

WOW, I would have never figured that out myself, thanks!

I had a different error though, a "local variable named 'xxx' cannot be declared in this scope..."

# August 30, 2009 12:49 AM

stimul8d said:

pretty poor form from the compiler though it should have just been...

List<Whatever> list = // get list

outputList = list.FindAll(delegate(Whatever item)

{

  return (//blah blah);

}

);

# November 16, 2009 12:11 PM

Travis Haws said:

Wow thanks for this.

# January 26, 2010 5:41 PM

Fender jAzz Bass said:

"Hi there, I found your blog site in a new directory of blogs. I dont know how your web site arrived up, must are already a typo, Your weblog appears beneficial. Have a good day."

--------------------------------------------

my website is  

http://signedbaseball.info

Also welcome you!

# November 24, 2010 2:13 PM

HarMonica Tabs said:

I recently came throughout your web site and take place to be understanding along. I assumed I'd personally leave my initial comment. I really do not know what  to say except that We've loved examining. Respectable web website. I'm going to maintain visiting this weblog seriously frequently.

--------------------------------------------

my website is  

http://yogaball.us

Also welcome you!

# December 3, 2010 2:51 AM

skateboard cupcakes said:

"How-do-you-do, just needed you to know I've added your website to my Yahoo bookmarks because of your extraordinary blog site layout. But severely, I think  your internet site has 1 with the freshest theme I've came across. It really helps make studying your blog page a great deal easier."

--------------------------------------------

my website is <a href="zeroskateboards.org/.../cool-skateboards-images-10.html">zero skateboards</a> .Also welcome you!

# December 3, 2010 10:20 AM

google voice ipad app said:

To be both a speaker of words and a doer of deeds.

-----------------------------------

# December 18, 2010 4:45 AM

best ipad accessories said:

-----------------------------------------------------------

"Nice article! GA can also be my greatest incomes. Nevertheless, it’s not a a lot."

# January 4, 2011 4:37 AM

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."

# January 9, 2011 3:48 AM

television reviews said:

"In seeking for web-sites associated to net hosting and specifically comparison internet hosting linux strategy internet,your internet site arrived up."

# February 10, 2011 3:06 PM

weblogs.asp.net said:

Bad compiler warnings when using anonymous methods for Predicate_3C00_T_3E00_.. He-he-he :)

# April 2, 2011 6:51 AM

weblogs.asp.net said:

Bad compiler warnings when using anonymous methods for Predicate_3C00_T_3E00_.. Smashing :)

# June 11, 2011 8:16 PM

Santo Horan said:

Thanks for the recommendations shared in your weblog. 1 more thing I would like to convey is that weight loss isn't about going on a celebrity diet plan and attempting to decrease as a lot weight that you may in a couple of days. Probably the most powerful method to burn fat is by employing it slowly and gradually and ideal right after some simple recommendations which can allow you to make essentially the most from a attempt to lose weight. You could recognize and be following a few of these ideas, nevertheless reinforcing info never affects.

# July 1, 2011 1:45 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)