See the Mentor code in the enumeration from the 0.1.2.4 start assignment, puzzled so Baidu a bit

Source: Internet
Author: User

Why do enumeration values in C # Use the power of 1,2,4,8,16,32, such as 2, to use 1,2,4,8,16,32, such as 2, to prevent duplication.
. NET enumeration we generally have two usages, one representing the unique sequence of elements, such as the days of the week, and the states that represent multiple compounds. This time, it is generally necessary to mark the enumeration with the [Flags] attribute as a bit field, for example:
[Flags]
Enum styles{
Showborder = 1,//Whether the border is displayed
Showcaption = 2,//whether the caption is displayed
Showtoolbox = 4//Whether the Toolbox is displayed
}
This allows us to combine multiple states with the "or" operator, for example
Mycontrol.style = Styles.showborder | Styles.showcaption;
At this point the value of the Mycontrol.style enumeration becomes 1+2=3, and its ToString () becomes "Styles.showborder, styles.showcaption"
Here we can explain why the third value Showtoolbox can be 4,5: and cannot be 3. That is, its value should not be a compound value of the first few items. There is a relatively simple method is to use 2 of the N-order for each assignment, such as 1,2,4,8,16,32,64 ....
Now give a common example of the flags application. For example, a simple permission system, with "Admin" and "User" roles, we can put a varchar () field in the table, the permission word "admin,user" in text form. But with the flags enumeration, we can direct the Roles.admin | The value of the roles.user is placed in an int field.
The following are some common operations on enumerations:
To change the value of an enumeration back to an enumeration object:
Styles style = (styles) Enum.parse (typeof (Styles), 4); style = Styles.toolbox;
Check to see if an enumeration contains an element:
BOOL Hasflag = ((Style & styles.showborder)! = 0);
In fact, we will encounter a situation, that is, we need to remove an element from the combined state. You can do this with the "^" operator:
Styles style = Styles.showborder | Styles.showcaption;
style = style ^ Styles.showborder;
At this point, the value of the style will become styles.showcaption.
But here's a very serious problem (I just found out now)
We'll do it again this time.
style = style ^ Styles.showborder;
According to our assumption, this time the value of the style is styles.showcaption, does not contain styles.showborder, so we even remove this element, the style should still not change. But the value of the actual style becomes Styles.showborder | Styles.showcaption!! Once again, this element is removed, and the cycle is repeated.
Of course, we can do some checking before we get rid of an element, if the enumeration contains this element, then remove it:
if (style & styles.showborder)! = 0) {
style = style ^ Styles.showborder;
}
I wonder if there is any other way to easily remove an element from the Flags enumeration state:
Thanks to mobilebilly:
style = style & (~styles.showborder) makes it easy to remove an element.
(2) Enumeration members
An enumeration member is a named constant of the enumeration type. Each enumeration member name is unique and has an associated constant value, and the type of this value is the underlying type of the enumeration. The constant value of each enumeration member must be within the range of the underlying type of the enumeration.
Example:
public enum Timeofday:uint
{
Morning=-3,
Afternoon=-2,
Evening=-1
}
A compile-time error occurs because the constant value-1,-2, and –3 are not in the range of the underlying integer uint.
Note: It is necessary to annotate all the members of each sentence, otherwise people will see so many words in your members that you do not know or even how painful the abbreviation is ...
(3) enumeration member assignment
Rule: Allow multiple enumeration members to have the same value, allow no sequential definition of member values, the value of a member without an explicit assignment defaults to the previous member's value +1 (the first member defaults to 0 if not assigned). In addition, the enumeration value cannot exceed its underlying type range.
Example
public enum Color
{
Red = 1,
Green
Orange = 1,
Grey = 5,
Purple
}
The value of green above is 2, while the value of purple is 6. Red and orange are 1.
Note: It is because of the default +1 such rules, it is recommended that each member manually assigned value, so as to prevent the program to write half, from the previous changes or add members, the subsequent member of the default values will change, this is troublesome.
(4) Conversion of enum type to underlying type
An underlying type cannot be implicitly converted to an enumeration type
Enum types are also not implicitly convertible to the underlying type
Example:
public enum number
{
A
B
}
Class Test
{
public static void Main ()
{
int i = number.a;//error, to enforce type conversion (int) NUMBER.A
Number n;
n = 2;//error, to force type conversion (number) 2
}
}
(5) Use of enumeration types
Use in switch (TimeofDay)//switch
{
Case timeofday.morning:
Console.WriteLine ("Good Morning");
Break
...... The rest is omitted.
}
int hashcode = TimeofDay.Morning.GetHashCode ();//Gets the value of the enumeration member
String str = timeofDay.Morning.toString ();//Gets the name of the enumeration member
String enumparsestr = Enum.parse (typeof (TimeofDay), hashcode.tostring ()). ToString ();//Converts the value of the enumeration to the corresponding name
int enumparseint = Convert.ToInt32 (Enum.parse (typeof (Timeoftime), str));//Convert the name of the enumeration to the corresponding value

See the Mentor code in the enumeration from the 0.1.2.4 start assignment, puzzled so Baidu a bit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.