| Start |
|---|
| Enum as flags The Because Although flags are often only a single bit, they can be combined into named "sets" for easier use. To avoid spelling out the decimal values of powers of two, the left-shift operator (<<) (opens new window) can also be used to declare the same enum Starting with C# 7.0, binary literals (opens new window) can be used too. To check if the value of enum variable has a certain flag set, the And a With Also we can iterate through all values of enum to get all flags that are set Or All three examples will print: |
| Enum basics From MSDN (opens new window): An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable. Essentially, an enum is a type that only allows a set of finite options, and each option corresponds to a number. By default, those numbers are increasing in the order the values are declared, starting from zero. For example, one could declare an enum for the days of the week: That enum could be used like this: By default the underlying type of each element in the The numbers after the name are now bytes instead of integers. You could get the underlying type of the enum as follows: Output: |
| Using << notation for flags The left-shift operator ( This also helps to improve readability of large enums with plenty of flags in them. It is obvious now that |
| Test flags-style enum values with bitwise logic A flags-style enum value needs to be tested with bitwise logic because it may not match any single value. The |
Enum to string and back |
| Add and remove values from flagged enum This code is to add and remove a value from a flagged enum-instance: |
| Default value for enum == ZERO The default value for an enum is zero. If an enum does not define an item with a value of zero, its default value will be zero. |
| Adding additional description information to an enum value In some cases you might want to add an additional description to an enum value, for instance when the enum value itself is less readable than what you might want to display to the user. In such cases you can use the For example: Now, if you would like to return the description of a specific enum value you can do the following: This can also be easily transformed to an extension method for all enums: And then easily used like this:
|
| Enums can have unexpected values Since an enum can be cast to and from its underlying integral type, the value may fall outside the range of values given in the definition of the enum type. Although the below enum type There is currently no way to define an enum which does not have this behavior. However, undefined enum values can be detected by using the method |
Get all the members values of an enum This will print: |
| Bitwise Manipulation using enums The FlagsAttribute (opens new window) should be used whenever the enumerable represents a collection of flags, rather than a single value. The numeric value assigned to each enum value helps when manipulating enums using bitwise operators. Example 1 : With [Flags] prints Red,Blue Example 2 : Without [Flags] prints 3 Syntax
RemarksAn Enum (short for "enumerated type") is a type consisting of a set of named constants, represented by a type-specific identifier. Enums are most useful for representing concepts that have a (usually small) number of possible discrete values. For example, they can be used to represent a day of the week or a month of the year. They can be also be used as flags which can be combined or checked for, using bitwise operations. |
Read Article
https://develop.horje.com/learn/1434/reference