2 u012432750 u012432750 于 2013.11.11 20:58 提问

求高手解决C#编程,谢谢啊

namespace A
{
public class EnumTest
{
enum Days { saturday = 0, sunday, monday, tuesday, wednesday, thursday, friday, };
[FlagsAttribute]
enum Colors { red, blue, yellow };
static void Main()
{
Type weekdays = typeof(Days);
Console.WriteLine("对应于:");
foreach (string s in Enum.GetNames(weekdays))
Console.WriteLine("{0,11}={1}", s, Enum.Format(weekdays, Enum.Parse(weekdays, s), "d"));
Colors mycolors = Colors.red | Colors.blue | Colors.yellow;
Console.WriteLine("枚举变量mycolor 存放如下颜色组合:{0}", mycolors);
Console.ReadLine();
}
}
}
怎么不出来red

2个回答

xia4o
xia4o   2015.08.14 15:38

改成这样enum MyColors { Red = 0x1, Blue = 0x2, Yellow = 0x4 };

xia4o
xia4o   2015.08.14 15:42

enum枚举成员可以用来作为位标志
但你的值必须按照位来设置:

[Flags]
enum CardDeckSettings
{  
    SingleDeck = 0x01,      // Bit 0  
    LargePictures = 0x02,   // Bit 1  
    FancyNumbers = 0x04,    // Bit 2  
    Animation = 0x08        // Bit 3      
}  
Csdn user default icon
上传中...
上传图片
插入图片