In C# why can't a conditional operator implicitly cast to a nullable type

Posted by Tim Jarvis on Stack Overflow See other posts from Stack Overflow or by Tim Jarvis
Published on 2008-10-20T23:04:11Z Indexed on 2010/04/07 10:13 UTC
Read the original article Hit count: 199

I am curious as to why an implicit cast fails in...

int? someValue = SomeCondition ? ResultOfSomeCalc() : null;

and why I have to perform an explicit cast instead

int? someValue = SomeCondition ? ResultofSomeCalc() : (int?)null;

It seems to me that the compiler has all the information it need to make an implicit casting decision, no?

© Stack Overflow or respective owner

Related posts about c#

Related posts about conditional-operator