Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional operator cannot cast implicitly?

I'm a little stumped by this little C# quirk:

Given variables:

Boolean aBoolValue; Byte aByteValue; 

The following compiles:

if (aBoolValue)      aByteValue = 1;  else      aByteValue = 0; 

But this will not:

aByteValue = aBoolValue ? 1 : 0; 

Error says: "Cannot implicitly convert type 'int' to 'byte'."

And of course, this monstrosity will compile:

aByteValue = aBoolValue ? (byte)1 : (byte)0; 

What's going on here?

EDIT:

Using VS2008, C# 3.5

like image 407
MPelletier Avatar asked Feb 07 '10 03:02

MPelletier


2 Answers

This is a fairly frequently asked question.

In C#, we almost always reason from inside to outside. When you see

x = y; 

we work out what is the type of x, what is the type of y, and whether the type of y is assignment compatible with x. But we do not use the fact that we know what the type of x is when we are working out the type of y.

That's because there might be more than one x:

void M(int x) { } void M(string x) { } ... M(y); // y is assigned to either int x or string x depending on the type of y 

We need to be able to work out the type of an expression without knowing what it is being assigned to. Type information flows out of an expression, not into an expression.

To work out the type of the conditional expression, we work out the type of the consequence and the alternative expressions, pick the more general of the two types, and that becomes the type of the conditional expression. So in your example, the type of the conditional expression is "int", and it is not a constant (unless the condition expression is constant true or constant false). Since it is not a constant, you can't assign it to byte; the compiler reasons solely from the types, not from the values, when the result is not a constant.

The exception to all these rules is lambda expressions, where type information does flow from the context into the lambda. Getting that logic right was very difficult.

like image 93
Eric Lippert Avatar answered Sep 24 '22 01:09

Eric Lippert


I'm using VS 2005, for and I can reproduce, for bool & Boolean, but not for true

 bool abool = true;  Boolean aboolean = true;  Byte by1 = (abool ? 1 : 2);    //Cannot implicitly convert type 'int' to 'byte'  Byte by2 = (aboolean ? 1 : 2); //Cannot implicitly convert type 'int' to 'byte'  Byte by3 = (true ? 1 : 2);     //Warning: unreachable code ;) 

The simplest workaround seems to be this cast

 Byte by1 = (Byte)(aboolean ? 1 : 2); 

So, yes, it seems that the ternary operator is causing the constants to "fix" their types as ints and disable the implicit type conversion that you would otherwise get from constants that fit within the smaller type.

like image 44
John Knoeller Avatar answered Sep 21 '22 01:09

John Knoeller