この C コードを見てください。
int main()
{
unsigned int y = 10;
int x = -2;
if (x > y)
printf("x is greater");
else
printf("y is greater");
return 0;
}
/*Output: x is greater.*/
コンピューターが両方を比較すると、x が符号なし整数型に昇格されるため、出力が x の方が大きい理由を理解しています。x が符号なし整数に昇格すると、-2 は 65534 になり、これは明らかに 10 より大きくなります。
しかし、なぜ C# では、同等のコードが反対の結果をもたらすのでしょうか?
public static void Main(String[] args)
{
uint y = 10;
int x = -2;
if (x > y)
{
Console.WriteLine("x is greater");
}
else
{
Console.WriteLine("y is greater");
}
}
//Output: y is greater.