Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This works:

short value;
value = 10 > 4 ? 5 : 10;

This works:

short value;
value = "test" == "test" ? 5 : 10;

This doesn't work:

short value;
string str = "test";
value = "test" == str ? 5 : 10;

Neither does this:

short value;
string str = "test";
value = "test".Equals(str) ? 5 : 10;

The last two cases I am getting the following error:

Cannot implicitly convert type 'int' to 'short'.
An explicit conversion exists (are you missing a cast?)

Why do I have to cast on the last two cases and not on the two first cases?

share|improve this question
    
@Tyler: indeed, sorry removed the close vote. The duplicate one I gave was not specific. stackoverflow.com/questions/3678792/… States the difference between compile-time (==) and run-time (equals). Also 10 > 4 will be optimized out by the compiler where a variable is most of the time checked run-time (of not a constant) –  RvdK yesterday
1  

3 Answers 3

up vote 41 down vote accepted
short value;
value = 10 > 4 ? 5 : 10;             //1
value = "test" == "test" ? 5 : 10;   //2
string str = "test";
value = "test" == str ? 5 : 10;      //3
value = "test".Equals(str) ? 5 : 10; //4

The last two ternary expressions (3,4) cannot be resolved to a constant at compile time. Thus the compiler treats the 5 and 10 as int literals, and the type of the entire ternary expression is int. To convert from an int to a short requires an explicit cast.

The first two ternary expressions (1,2) can be resolved to a constant at compile time. The constant value is an int, but the compiler knows it fits in a short, and thus does not require any casting.

For fun, try this:

value = "test" == "test" ? 5 : (int)short.MaxValue + 1;
share|improve this answer
    
+1 Finally! :-) –  Groo yesterday
4  
finally a correct answer –  Selman22 yesterday
4  
For reference: this behavior implemented by the compiler is called Constant Folding –  Bas Brekelmans yesterday
1  
@Groo -- You know it took less than 10 mins for the correct answer. –  Hogan yesterday
1  
Here's the MS Documentation that says this can happen. "A constant expression of type int can be converted to sbyte, byte, short, ushort, uint, or ulong, provided the value of the constant expression is within the range of the destination type." But this doesn't really explain why the compiler can't figure out that both 5 and 10 can be shorts, other than the obvious "because it can't". –  ahruss yesterday

You need a cast to make the two last examples work

value = (short)("test" == str ? 5 : 10);

Why dont't you need it in the first two?

Because the first two are compile-time constants. The compiler is able to translate 10 > 4 ? 5 : 10 to true ? 5 : 10, then to just 5

So when you write

value = 10 > 4 ? 5 : 10;

It's effectively the same as

value = 5;

which compiles because the compiler is allowed to implicitly cast constants if they are in the allowed range.

Conversely, "test" == str ? 5 : 10; is not a compile time constant, so the compile is not allowed to implcitly cast it. You need to make an explicit cast yoursef.

share|improve this answer

The thing is that 10 > 4 ? 5 : 10; is actually converted to a constant at compile time before any type casting is needed. Meaning the compiler realized that the turnary statement can actually be reduced to a constant even before any implicit type casting is required for the compilation. So in other words that expression is the same as:

value = 5;

In the last two statements, that is not true since you are using a variable to hold the values for you and not a constant. The compiler doesn't check the actual value of the variable to see if it can reduce the expression to a constant. So you actually need the casting.

share|improve this answer
1  
Isn't it the same as value = 5 instead of 10? –  Falanwe yesterday
    
Right. Wrote this in a hurry. Fixed. –  Farhad AliNoo 23 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.