14
C# – Flags 形式的 Enum
source link: https://naclyen.com/2020/04/14/c-flags-%e5%bd%a2%e5%bc%8f%e7%9a%84-enum/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
十進位表示法
[Flags]
enum Days
{
None = 0, // 0000 0000
Sunday = 1, // 0000 0001
Monday = 2, // 0000 0010
Tuesday = 4, // 0000 0100
Wednesday = 8, // 0000 1000
Thursday = 16, // 0001 0000
Friday = 32, // 0010 0000
Saturday = 64 // 0100 0000
}
位元移位表示法
[Flags]
enum Days
{
None = 0, // 0000 0000
Sunday = 1 << 0, // 0000 0001
Monday = 1 << 1, // 0000 0010
Tuesday = 1 << 2, // 0000 0100
Wednesday = 1 << 3, // 0000 1000
Thursday = 1 << 4, // 0001 0000
Friday = 1 << 5, // 0010 0000
Saturday = 1 << 6 // 0100 0000
}
二進位表示法 (C# 7.2新增)
[Flags]
enum Days
{
None = 0b_0000_0000, // 0000 0000
Sunday = 0b_0000_0001, // 0000 0001
Monday = 0b_0000_0010, // 0000 0010
Tuesday = 0b_0000_0100, // 0000 0100
Wednesday = 0b_0000_1000, // 0000 1000
Thursday = 0b_0001_0000, // 0001 0000
Friday = 0b_0010_0000, // 0010 0000
Saturday = 0b_0100_0000 // 0100 0000
}
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK