Possible Duplicate:
Why is this code invalid in C#?
Conditional operator cannot cast implicitly?
If I do the following:
bool someBool = false;
uint value = 0;
These are fine:
value = (someBool) ? 0 : (uint)1;
value = (someBool) ? (uint)0 : 1;
But this is not:
value = (someBool) ? 0 : 1;
Why can I not use the last one when I can easily say:
value = 0;
value = 1;
How is the type of the ternary operator determined?
My short summary :
The compiler will try if
bis convertible to the type ofa, otherwise ifais convertible to the type ofb. When neither is possible there is an error.But in
the ternary operator simply return
int(bothaandbare ints here).The variable on the left side of the assignment is not considered when determining the type.