我在 C# 编译器中发现了一个奇怪的情况。为什么需要下面的演员表?
using System;
class Program
{
private const byte BIT_ZERO_SET = 1;
private const byte BIT_ONE_SET = 2;
private const byte BIT_TWO_SET = 4;
static void Main(string[] args)
{
byte b = BIT_ZERO_SET | BIT_ONE_SET;
Console.WriteLine(b);
//Does not compile, says needs to cast to int.
//b = b | BIT_TWO_SET;
//Compiles...ugly
b = (byte)(b | BIT_TWO_SET);
Console.WriteLine(b);
Console.WriteLine("Press enter.");
Console.ReadLine();
}
}
谢谢。