This article is a mirror article of machine translation, please click here to jump to the original article.

View: 26773|Reply: 0

[Source] C# enums use the Flags feature, where multiple values are stored in one enumeration variable

[Copy link]
Posted on 4/9/2018 10:40:44 PM | | | |
Previously, if we defined an enum type and then defined an enum variable, then this enum variable could only be one value in the type, now we want one variable to represent multiple values:

When I was reading "Pro Net 2.0 Windows Forms And Custom Cortrols In C#" today, I saw an enumeration section, and I found that I needed to merge multiple values in an enum, and I saw that "|" was used. operator, I didn't pay much attention to it before, but today I thought about why I used "|" What about it?

I saw this sentence in MSDN: "Enum constants are defined by powers of 2 (i.e. 1, 2, 4, 8, etc.). This means that the individual flags in the combined enum constants do not overlap. ”

The enum constant must be defined by powers of 2 (i.e., 1, 2, 4, 8, etc.). This means that the individual flags in the combined enum constants do not overlap.



If you can have more than one value for a value, you can use an enum and add Flags

This article tells you how to write a Flags.

Before writing, you need to know some basic knowledge, take the opposite, or, and, if you don't know, please take a look at the basics.

Of course, these are too complicated for me to explain here.

If there is a type

Merge multiple values

To merge multiple of them, use |

Determine if a value exists

An easy way to do this is with HasFlag, but one way is to use &

Remove a value

Take the opposite value







Previous:Windows system mysql-5.7.21 installation tutorial
Next:Check the Windows server shut-on logs
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com