向 Enum 类型变量添加多个值
http://msdn.microsoft.com/en-us/library/cc138362.aspx
我引用了上面链接中的代码,该代码显示了向枚举类型添加值。 但它在我这边不起作用。 代码预期输出: 我得到实际输出:
请参考代码: **
// 预期输出:会议日为星期二、星期四、星期五
// 实际输出:会议日为星期五
// 预期输出:会议日为星期四、星期五
// 实际输出:会议日是星期一**
class Program
{
enum Days2
{
None = 1,
Sunday = 2,
Monday = 3,
Tuesday = 4,
Wednesday = 5,
Thursday = 6,
Friday = 7,
Saturday = 8
}
static void Main(string[] args)
{
Days2 meetingDays = Days2.Tuesday | Days2.Thursday;
// Initialize with two flags using bitwise OR.
meetingDays = Days2.Tuesday | Days2.Thursday;
// Set an additional flag using bitwise OR.
meetingDays = meetingDays | Days2.Friday;
Console.WriteLine("Meeting days are {0}", meetingDays);
// Expected Output: Meeting days are Tuesday, Thursday, Friday
**// Actual Output: Meeting days are Friday**
// Remove a flag using bitwise XOR.
meetingDays = meetingDays ^ Days2.Tuesday;
Console.WriteLine("Meeting days are {0}", meetingDays);
// Expected Output: Meeting days are Thursday, Friday
**// Actaul Output: Meeting days are Monday**
Console.ReadLine();
}
}
http://msdn.microsoft.com/en-us/library/cc138362.aspx
I refer a code from above link which is showing adding values to Enum Type.
but its not working at my end.
The code is expected output :
I am getting actual output:
Please refer code :
**
// Expected Output: Meeting days are Tuesday, Thursday, Friday
// Actual Output: Meeting days are Friday
// Expected Output: Meeting days are Thursday, Friday
// Actaul Output: Meeting days are Monday**
class Program
{
enum Days2
{
None = 1,
Sunday = 2,
Monday = 3,
Tuesday = 4,
Wednesday = 5,
Thursday = 6,
Friday = 7,
Saturday = 8
}
static void Main(string[] args)
{
Days2 meetingDays = Days2.Tuesday | Days2.Thursday;
// Initialize with two flags using bitwise OR.
meetingDays = Days2.Tuesday | Days2.Thursday;
// Set an additional flag using bitwise OR.
meetingDays = meetingDays | Days2.Friday;
Console.WriteLine("Meeting days are {0}", meetingDays);
// Expected Output: Meeting days are Tuesday, Thursday, Friday
**// Actual Output: Meeting days are Friday**
// Remove a flag using bitwise XOR.
meetingDays = meetingDays ^ Days2.Tuesday;
Console.WriteLine("Meeting days are {0}", meetingDays);
// Expected Output: Meeting days are Thursday, Friday
**// Actaul Output: Meeting days are Monday**
Console.ReadLine();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了能够使用按位运算来组合枚举值,它们需要具有与 2 的幂相对应的值。
此外,您应该使用
Flags
属性来标记枚举,并按照约定使用零作为“未设置标志”。示例:有关详细信息,请参阅 C# 编程指南 。
In order to be able to use bitwise operations to combine enum values, they need to have values that correspond to powers of two.
Additionally, you should mark the enum with the
Flags
attribute, and by convention use zero as "no flags set". Example:See the C# Programming Guide for more info.
将 [Flags] 属性添加到您的枚举中。
Add [Flags] attribute to your enum.
您忘记使用 FlagsAttribute!
You forgot to use FlagsAttribute!
查看 Days2.Tuesday | 的值Days2.Thursday; 应为 10。按位 AND 本质上是加法。
如果您想要几天,请按照二进制样式进行操作:
或者按照其他人所述使用 Flags 属性。
Look at the value of
Days2.Tuesday | Days2.Thursday;
It should be 10. A bitwise AND is essentially an addition.If you want several days, do them this way, binary style:
Or use the Flags attribute as others stated.
使用
[Flags]
属性标记枚举。Mark the enum with
[Flags]
attribute.