0

我正在尝试在 C# 中学习枚举。当我阅读一些文档时,我编写了一个程序。但我不明白这个程序是如何工作的。有人可以帮我看看它的工作吗?

我在下面发布我的代码:

程序 1:

public partial class MainPage {
    [Flags]
    enum Days {
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday ,
        Saturday,
        Sunday
    };

    // Constructor
  public MainPage() {
        InitializeComponent();
        Days holidays = Days.Sunday | Days.Saturday;
        if ((Days.Sunday | holidays) == Days.Sunday) // This returns true. Why ?
            MessageBox.Show("True");
        else
            MessageBox.Show("False");
  }

方案二:

在这里,我在枚举中分配整数值

public partial class MainPage {
    [Flags]
    enum Days {
        Monday = 1,
        Tuesday = 2,
        Wednesday = 5,
        Thursday = 8,
        Friday = 10,
        Saturday = 3,
        Sunday = 0
    };

    // Constructor
    public MainPage() {
        InitializeComponent();
        Days holidays = Days.Sunday | Days.Saturday;
        if ((Days.Sunday | holidays) == Days.Sunday) // Why this returns false ?
            MessageBox.Show("True");
        else
            MessageBox.Show("False");
  }

在程序2中分配整数值后,if条件有什么区别?

4

3 回答 3

3

默认情况下,枚举从 0 开始,例如:

[Flags]
enum Days
{
    Monday = 0,
    Tuesday = 1,
    Wednesday = 2,
    Thursday = 3,
    Friday = 4,
    Saturday = 5,
    Sunday = 6
};

我认为您对枚举上的按位运算符感到困惑。通常要使枚举与按位运算符一起使用,您可以像这样声明它们:

[Flags]
enum Days
{
    Monday = 0,
    Tuesday = 1,
    Wednesday = 2,
    Thursday = 4,
    Friday = 8,
    Saturday = 16,
    Sunday = 32
};
于 2013-02-22T05:23:37.887 回答
2

看起来您将按位运算符|与逻辑运算符混淆了||

在您的原始程序中,星期六被分配 5,星期日被分配 6。

按位或:

8 4 2 1 
0 1 0 1 = 5 Saturday
0 1 1 0 = 6 Sunday
-----------
0 1 1 1 = 7 Result (Holiday).

按位或:

8 4 2 1 
0 1 1 1 = 7 Holidays
0 1 1 0 = 6 Sunday
-----------
0 1 1 1 = 7 Result.

但是我运行了类似的相同代码:

using System;

namespace ConsoleApplication1
{
  class Program
  {
    [Flags]
    enum Days
    {
        Monday,     //Default 0
        Tuesday,    //Default 1
        Wednesday,  //Default 3
        Thursday,   //Default 4
        Friday,     //Default 5
        Saturday,   //Default 6
        Sunday      //Default 7
    };

    static void Main(string[] args)
    {
        Days holidays = Days.Sunday | Days.Saturday;
        if ((Days.Sunday | holidays) == Days.Sunday) // This returns true. Why ?
            Console.WriteLine("True");
        else
            Console.WriteLine("False");

        Console.ReadKey();
    }
  }
}

结果:(False与您的示例不同)。

正如预期的那样,7 不等于 6。 我的假设是您的示例中缺少代码或输入错误的代码。

如果您正确使用它,我强烈建议您阅读FlagsAttribute应该如何工作,这涉及在位值(1、2、4、8、16、32)处创建枚举。

如果您的代码已更正,那么这可能更符合我认为您正在寻找的内容。

using System;

namespace ConsoleApplication1
{
  class Program
  {
    [Flags]
    enum Days
    {
        Monday    = 1,
        Tuesday   = 2,   
        Wednesday = 4, 
        Thursday  = 8, 
        Friday    = 16,   
        Saturday  = 32, 
        Sunday    = 64
    };

    static void Main(string[] args)
    {
        Days holidays = Days.Sunday | Days.Saturday;

        if ((Days.Sunday | holidays) == holidays)
            Console.WriteLine("Sunday is a Holiday");
        else
            Console.WriteLine("Sunday is a Holiday");

        if ((Days.Tuesday | holidays) == holidays)
            Console.WriteLine("Tuesday is not a Holiday");
        else
            Console.WriteLine("Tuesday is not a Holiday");

        Console.ReadKey();
    }
  }
}

结果:Sunday is a Holiday Tuesday is not a Holiday

这是有效的,因为,

按位或:

64 32 16  8  4  2  1 
 0  1  0  0  0  0  0 = 32 Saturday
 1  0  0  0  0  0  0 = 64 Sunday
----------------------
 1  1  0  0  0  0  0 = 96 Result (Holiday).

按位或:

64 32 16  8  4  2  1 
 1  1  0  0  0  0  0 = 96 Holiday
 1  0  0  0  0  0  0 = 64 Sunday
----------------------
 1  1  0  0  0  0  0 = 96 Result (Holiday).

按位或:

64 32 16  8  4  2  1 
 1  1  0  0  0  0  0 = 96 Holiday
 0  0  0  0  0  1  0 = 32 Tuesday
----------------------
 1  1  0  0  0  1  0 = 97 Result (Not Holiday).
于 2013-02-22T05:29:30.470 回答
2

为了补充杰里米的答案,考虑到他对 Days 的第二个(也是正确的)定义二进制表示是:

0000 0001   Monday
0000 0010   Tuesday
0000 0100   Wednesday
0000 1000   Thursday
0001 0000   Saturday
0010 0000   Sunday

接下来您执行以下操作

var holidays = Days.Sunday | Days.Saturday;

这执行按位或:

      0001 0000   Saturday
OR    0010 0000   Sunday
      --------------------
      0011 0000   Saturday | Sunday

接下来检查以下内容

((Days.Sunday | holidays) == Days.Sunday)

意思是:

   0010 0000   Sunday
OR 0011 0000   holidays  (= Saturday | Sunday)
   ---------
   0011 0000   This is still holidays and does not equal Sundays

如果你想检查 'holidays' 是否设置了星期天位,你应该使用按位 AND

((Days.Sunday & 节假日) == Days.Sunday)

    0010 0000   Sunday
AND 0011 0000   holidays  (= Saturday | Sunday)
    ---------
    0010 0000   Sunday

另请注意,较新版本的 .NET Framework 内置了对标志检查的支持:

var holidays = Days.Sunday | Days.Saturday
holidays.HasFlag(Days.Sunday)  //Will return true
holidays.HasFlag(Days.Monday)  //Will return false

您对 Days 枚举的定义不应与 [Flags] 一起使用,如果您将本文中的相同逻辑应用于定义中的值,您最终会得到一些非常奇怪的结果。

于 2013-02-22T05:40:35.280 回答