Conditional operator cannot cast implicitly?

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.

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.

My mind is blown. Thank you, sir, that is one part enlightening and two parts frustrating. One because it can't work the way it would seem to work, and two because it makes sense that it can't work that way.So... constants it is!

– MPelletier Feb 7 '10 at 5:21 Thanks. I was looking for a post on this topic on your blog, but this is even better. – John Knoeller Feb 7 '10 at 6:31 @John: I talk about these issues a bit here: blogs.msdn.Com/ericlippert/archive/2006/05/24/… – Eric Lippert Feb 7 '10 at 15:11.

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.

1 for making a cast less ugly than mine :) – MPelletier Feb 7 '10 at 3:31 1 Your explanation makes sense. But why would the constants not fix in this circumstance? And doubly curious with Mendy's discovery.

Someone at Microsoft should know, and might need to be argued with strongly... – MPelletier Feb 7 '10 at 3:37 I think Mendy discovered that when compiler can trivially detect that it can optimize the ternary operator away entirely, it the compiles code that is equal to Byte by = 2 which preserves the ability to implicitly cast. (see my comment on the compiler warning above) – John Knoeller Feb 7 '10 at 3:43 I think Eric Lippert may have discussed the why of this already on his blog. – John Knoeller Feb 7 '10 at 3:44 1 @Mendy: Found it blogs.msdn.Com/ericlippert/archive/2006/05/24/… – John Knoeller Feb 7 '10 at 19:59.

I may not have a great answer for you, but if you do this in many places, you could declare: private static readonly Byte valueZero = (byte)0; private static readonly Byte valueOne = (byte)1; and just these variables. You might get away with using const if it is local to the project. EDIT: using readonly would not make sense - these aren't ever meant to change.

I'm sure it was just a typo but you declared the same variable twice. – Cory Charlton Feb 7 '10 at 3:20 1 @Hamish: I see what you mean. I could use const, but it's a workaround.

This certainly doesn't deserve the vote down you got though. – MPelletier Feb 7 '10 at 3:22.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions