简体   繁体   English

使用运算符和| 在C#的枚举?

[英]Using operators & | on enums in C#?

I have an enum in my application to represent the saving options where the user can save the image with lines drawn, circles, rectangles or any combination, so I declared an enum to represent the saving option. 我的应用程序中有一个用于表示保存选项的枚举,用户可以使用绘制的线条,圆形,矩形或任意组合保存图像,因此我声明了一个枚举来表示保存选项。

enum SaveOption{lines,circles,rectangles};

How can I use operators to; 我怎样才能使用运算符;

  • Add option to the options 添加选项选项
  • Remove option from the options 从选项中删除选项

Mark the enum with the [Flags] attribute, and give each possible value a unique bit value: 使用[Flags]属性标记枚举,并为每个可能的值赋予唯一的位值:

[Flags]
enum SaveOption
{
    lines = 0x1,
    circles = 0x2,
    rectangles = 0x4
}

Then you can do this sort of thing: 然后你可以做这样的事情:

SaveOption options;

option = SaveOption.lines | SaveOption.circles; // lines + circles
option |= SaveOptions.rectangles; // now includes rectangles
option &= ~SaveOptions.circles; // now excludes circles

Finally for ref, each option must have a value that is represented by a single bit, so in hex that's 0x1 , 0x2 , 0x4 , 0x8 , then 0x10 , 0x20 , 0x40 , 0x80 etc. Which is easier to remember than 1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536. 最后,对于裁判,每个选项必须有一个是由单个位表示的值,所以在十六进制0x10x20x40x8 ,然后0x100x200x400x80等等,哪一样容易比1,2记住, 4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536。 Which is as far as I remember :) 这是我记得的:)

To start with, you'd need to specify values which worked in a bitwise fashion. 首先,您需要指定以按位方式工作的值。 You'd also be best to decorate the enum with FlagsAttribute . 你最好用FlagsAttribute来装饰枚举。 Following naming conventions, you'd also rename it to be plural. 遵循命名约定,您还将其重命名为复数。 You'd end up with: 你最终得到:

[Flags]
enum SaveOptions
{
    None = 0,
    Lines = 1,
    Circles = 2,
    Rectangles = 4
}

You can then do: 然后你可以这样做:

SaveOptions foo = SaveOptions.Lines | SaveOptions.Rectangles;

and similar bitwise operations. 和类似的按位运算。

The FlagsAttribute will change how the enum values are converted to and from strings. FlagsAttribute将更改枚举值与字符串之间的转换方式。 It's not actually required in order to get the bitwise operations to work, but it's definitely a strong convention, and a generally good idea. 实际上并不需要按位操作,但它绝对是一个强大的惯例,并且通常是个好主意。

Use the Flags attribute. 使用Flags属性。 Don't forget to specify unique bitwise values for each member of the enum: 不要忘记为枚举的每个成员指定唯一的按位值:

[Flags]
enum SaveOption
{
    lines = 1,
    circles = 2,
    rectangles = 4
}

You need a cast: 你需要一个演员:

(SaveOption) (lines | circles)

or 要么

SaveOption option = (SaveOption) (lines | circles);

(SaveOption) ((option & ~lines) | rectangle)

You also need to assign bit values to the enum members (as in some of the other answers). 您还需要为枚举成员分配位值(如在其他一些答案中那样)。

Note: I've left off some C# syntax, and this applies to .Net 2, later version may have cleaned up this syntax. 注意:我已经停止了一些C#语法,这适用于.Net 2,后来的版本可能已经清除了这种语法。

As for manipulating the flags; 至于操纵旗帜;

[Flags]
public enum SaveOption
{
    Lines = 1,
    Circles = 2,
    Rectangles = 4
}

static void Main(string[] args)
{
    SaveOption option = SaveOption.Circles | SaveOption.Rectangles;

    Console.WriteLine( option );

    option -= SaveOption.Circles;

    Console.WriteLine( option );

    option = option | SaveOption.Lines;

    Console.WriteLine(option);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM