向 Enum 类型变量添加多个值

发布于 2024-11-04 05:49:49 字数 1435 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夢归不見 2024-11-11 05:49:49

为了能够使用按位运算来组合枚举值,它们需要具有与 2 的幂相对应的值。

此外,您应该使用 Flags 属性来标记枚举,并按照约定使用零作为“未设置标志”。示例:

[Flags]
public enum Days2 
{
    None = 0,
    Sunday = 1
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64
}

有关详细信息,请参阅 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:

[Flags]
public enum Days2 
{
    None = 0,
    Sunday = 1
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64
}

See the C# Programming Guide for more info.

满地尘埃落定 2024-11-11 05:49:49

将 [Flags] 属性添加到您的枚举中。

Add [Flags] attribute to your enum.

川水往事 2024-11-11 05:49:49

查看 Days2.Tuesday | 的值Days2.Thursday; 应为 10。按位 AND 本质上是加法。

如果您想要几天,请按照二进制样式进行操作:

enum Days2
{
    None = 0,
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64    
}

或者按照其他人所述使用 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:

enum Days2
{
    None = 0,
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64    
}

Or use the Flags attribute as others stated.

心如荒岛 2024-11-11 05:49:49

使用 [Flags] 属性标记枚举。

[Flags]
enum Days2
{
    None = 0,
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64
}

Mark the enum with [Flags] attribute.

[Flags]
enum Days2
{
    None = 0,
    Sunday = 1,
    Monday = 2,
    Tuesday = 4,
    Wednesday = 8,
    Thursday = 16,
    Friday = 32,
    Saturday = 64
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文