Conditional operator cannot cast implicitly?
问题 I\'m a little stumped by this little C# quirk: Given variables: Boolean aBoolValue; Byte aByteValue; The following compiles: if (aBoolValue) aByteValue = 1; else aByteValue = 0; But this will not: aByteValue = aBoolValue ? 1 : 0; Error says: \"Cannot implicitly convert type \'int\' to \'byte\'.\" And of course, this monstrosity will compile: aByteValue = aBoolValue ? (byte)1 : (byte)0; What\'s going on here? EDIT: Using VS2008, C# 3.5 回答1: This is a fairly frequently asked question. In C#, we