I'm using MS Visual Studio Enterprise 2015 and there's a quirky issue I found. Now, I'm not asking for how to fix the error that arises, but I am wondering why C# is behaving in this manner.
This snippet comes from C# Interactive, but it behaves the same in the editor as well:
short num;
num = 0
//num = 0
num = 4
//num = 4
num = true ? 1 : 2
//num = 1
num = false ? 1 : 2
//num = 2
bool flag = true;
num = flag ? 1 : 2
The last line results in the error:
(1,7): error CS0266: Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?)
Basically, there's no problem assigning numbers to a short, like 0 or 4, and there's no problem literally interpreting ternary either until you give it a variable condition. So what's the deal?