Home Dashboard Directory Help
Search

Console.WriteLine in Visual Studio 2010, .NET Framework 4 has a bug by zeldafans


Status: 

Closed
 as By Design Help for as By Design


4
1
Sign in
to vote
Type: Bug
ID: 587258
Opened: 8/18/2010 10:48:07 AM
Access Restriction: Public
0
Workaround(s)
view
0
User(s) can reproduce this bug

Description

list<int?>.Foreach(Console.WriteLine) report error:
No overload for 'System.Console.WriteLine(object)' matches delegate 'System.Action<int?>

but Write code in this way:
int? a=null;
Console.WriteLine(a)

is OK...I hate it
Details
Sign in to post a comment.
Posted by Microsoft on 1/6/2011 at 5:15 AM
Thanks for reporting this issue you've encountered with Visual Studio!

Your example as written does not compile as there is no non-generic type in the BCL named just "List". I presume when you say List here you mean List<int?>.

In that case, the behavior you're seeing is by design. There is no overload of Console.WriteLine that takes int?, however there is one that takes int. Notice if you replace List<int?> with List<int> (and skip adding null) that things work fine.

As you point out, variance can sometimes save you here - if you replace this with List<cat>, then it also builds successfully, using the Console.WriteLine(object) overload. However, variance cannot navigate boxing conversions, and so without an explicit int? overload for Console.WriteLine, you're out of luck.

Alex Turner
Program Manager
Visual Basic and C# Compiler
Posted by Will Sullivan on 8/19/2010 at 7:44 AM
No prob, Zeld.
Posted by zeldafans on 8/18/2010 at 9:56 PM
i thought it would accept parameters that have less derived types (contravariance),but int? or int is not inherit from Object ,right?

thx,Will, next time i will go to StackOverflow.com
Posted by Microsoft on 8/18/2010 at 7:17 PM
Thank you for reporting the issue.
We are routing this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.These specialized experts will follow-up with your issue.
Posted by Microsoft on 8/18/2010 at 5:06 PM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)
Posted by Will Sullivan on 8/18/2010 at 12:20 PM
(new List<int?>{1,2,3}).ForEach(x=> Console.WriteLine(x));

ForEach takes a delegate. In this case, the lambda is cast to Action<int?> implicitly. This is not a bug. You were attempting to pass in a "method group".

I'd suggest, if you have questions like this, ask them at StackOverflow.com. Save the connect reports for actual bugs.
Sign in to post a workaround.