[FlagsAttribute]
enum Colors_1
{
Red = 1, Green = 2, Blue = 4, Yellow = 8
};
//测试
private void button1_Click(object sender, EventArgs e)
{
Colors_1 color_1 = Colors_1.Red | Colors_1.Green | Colors_1.Blue
| Colors_1.Yellow;
string strResult = color_1.ToString() + " " + ((int)color_1)
.ToString();
MessageBox.Show(strResult);
}
再写个例子试试:
晕,怎么没把颜色全部显示出来呀?
咦!3 + 4 = 7 刚好显示枚举值为3,4的两种颜色
再写一个例子呢?
输出结果:
居然会自动转换成相应的枚举值,厉害!
再来我加个枚举为7的值: