Enumeration provides Group constant values, which help to make members of a strong type and improveCodeReadability. In C #, useEnumTo declare enumeration.
Enumeration is dividedSimple EnumerationAndFlag Enumeration.
Basic syntax example
Enum day {sun, Mon, Tue, wed, Thu, Fri, sat };
The following format is also supported:
Enum day
{
Sun,
Mon,
Tue,
Wed,
Thu,
Fri,
Sat
};
Enumeration type
The enumerated types can be byte, sbyte, short, ushort, Int, uint, long, or ulong. If no type is specified, the default type is int. Example of the specified type:
Enum day: Byte{Sun, Mon, Tue, wed, Thu, Fri, sat };
Enumerated Value
By default, the value of the first enumeration number is 0, and the value of each enumeration number increases by 1 based on the value of the previous enumeration number. Of course, you can also specify it by yourself, for example:
Enum day {sun = 1, Mon, Tue, wed, Thu, Fri, sat };
Enum range {min = 0, max = 255 };
Enum range2 {min, M1 = 50, M2, max = 255 };// Min is 0, M2 is 51
Case Sensitive Enumeration
For example, the following enumeration has two enumeration numbers:
Enum enumtest {Sun, sun}; // despite this, we do not recommend this write.
Obtains the enumerated value.
Although enumeration has a type, type conversion is still required when the enumerated value is used.
Public partial class _ Enum: system. Web. UI. Page
{
Enum range {min = 0, max = 255 };
Protected void page_load (Object sender, eventargs E)
{
Response. Write (range. max); // The output is Max.
Response. Write ("<br> ");
Response. Write (((INT) range. Max). Tostring (); // The output is: 255
}
}
Enumeration cannot be placed in the function.
Enumeration can be at the same level as the class, or can be used as the class field, but cannot be placed in the function. You can add public modifiers.
The following are some suggestions for enumeration.
Enumeration is preferred, rather than static constants of the class.
For example:
Public static class day
{
Public static int sun = 1;
Public static int MON = 2;
Public static int Tue = 3;
//...
}
The following enumeration should be used:
Enum day {sun = 1, Mon, Tue, wed, Thu, Fri, sat };
If the parameters, return values, variables, and Other types can be enumeration, do not use other basic types.
For example:
Range r = range. Max;// Good
Int r = (INT) range. Max;// Not good
Enumeration name
Enumeration generally uses a combination of nouns or nouns. A simple enumeration uses singular, and a flag enumeration uses plural.
In most cases, you do not need to change the default Enumeration type.
In most cases, INT (system. int32) is used as the enumeration type.. Unless:
- Enumeration is a flag enumeration with more than 32 signs (INT type cannot be installed at this time ).
- Enumeration is very large and frequently used, so as to save space and use a type smaller than Int.
- You have to use other types.
Do not set a sentry in Enumeration
We may think that adding a sentinel to both ends of the enumeration means that when determining whether a number is in the enumeration, we only need to determine whether it is in the Sentinel. Unfortunately, we should not do this, which destroys the meaning of enumeration.
Enum day {Firstvalue,Sun, Mon, Tue, wed, Thu, Fri, Sat, Lastvalue}; // Firstvalue and lastvalue should be removed
- Simple EnumerationThe included values are not used for combination or bitwise comparison.
- Flag EnumerationA combination of bitwise OR operations should be used.
Simple Enumeration
The day and range mentioned above can be called simple enumeration because they cannot be combined.
Flag Enumeration
Pay attention to the design of flag enumeration.
- Specify flagsattri to indicate that enumeration can be processed as a bit field (that is, a group of flags.
- The values of each flag in enumeration should be assigned a value of 2, namely, 1, 2, 4, 8, 16, 32 ......
For example, if we are designing a Windows windowProgramThe window can be minimized, maximized, or closed. We want to display any combination, that is, we can display any 0 or one or more buttons.
If you useSimple Enumeration, Sort and combine by arrangement. We need to use 1 + 3 + 3 + 1 = 8 enumerative numbers. If there are not three buttons, but four buttons, the number of enumerations will increase. So this is unrealistic.
Why is simple enumeration impractical? Because simple enumeration cannot be combined, it can be easily solved by using flag enumeration.
[Flags]
Public Enum windowstyle
{
Minimum_button = 1, // The hexadecimal format is 0x0001
Maximum_button = 2,
Close_button = 4
}
When setting window styles, we use or to freely combine them:
Windowstyle Ws = windowstyle. minimum_button | windowstyle. close_button; // indicates both minimum_button and close_button.
This is why the value of a flag needs to be sorted by the power of 2. It is also why int type cannot be used when there are more than 32 flag.
Generally, we provide special enumerated values for common logo combinations.
The preceding window is still used as an example. In most cases, we need to display the three buttons, so we need to use them each time:
Windowstyle Ws = windowstyle. minimum_button | maximum_button | windowstyle. close_button;
We can modify the enumeration as follows:
[Flags]
Public Enum windowstyle
{
Minimum_button = 1,
Maximum_button = 2,
Close_button = 4,
All_button = 7
}
Add an all_button as the value of the first three flags. You can use all_button directly for use.
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Globalization;
Namespace Test
{
Public Enum style: int
{
Lenovo = 1,
Acer = 2,
Dell = 3
}
Public class publicbll
{
Private int _ itemtype;
Public static int itemtype
{
Get
{
Return _ itemtype;
}
Set {_ itemtype = value ;}
}
}
}
Publicbll. itemtype returns 1 or 2 or 3 of the int type.
If you determine whether the value is equal to an enumeration
If (publicbll. itemtype = (INT) style. Lenovo)
{
// Code segment
}