Why do I have to typecast an int in a ternary expression? [duplicate]
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Conditional operator cannot cast implicitly? I have run into a peculiar situation and want to know why I have to do it. I'm using .NET 3.5. This works: short foo; if (isValid) foo = -1; else foo = getFoo(); This does not work: short foo; foo = isValid ? -1 : getFoo(); I have to typecast -1: short foo; foo = isValid ? (short)-1 : getFoo(); What does the ternary expression do differently? It considers the -1 to be