Enum class (msdn)

Source: Internet
Author: User
Tags mscorlib
Enum class (msdn)

Posted on ottox read (92) Comments (0 ){
Open_link ('HTTP: // www.cnblogs.com/ottox/admin/editposts.aspx? Postid = 1402424 ')
} "Rel =" nofollow "href =" http://writeblog.csdn.net/# "> edit {
Addtowz( 1402424); Return false;
} "Href =" http://writeblog.csdn.net/# "> favorites

Provides a base class for enumeration.

Namespace:System
Assembly:Mscorlib (in mscorlib. dll)

Syntax

Visual Basic (Declaration)
<SerializableAttribute> _<ComVisibleAttribute(True)> _Public MustInherit Class Enum _Inherits ValueType _Implements IComparable, IFormattable, IConvertible
C #
[SerializableAttribute][ComVisibleAttribute(true)]public abstract class Enum : ValueType,IComparable, IFormattable, IConvertible
Remarks

Enumeration is a specified constant. Its basic type can be any integer except char. If the basic type is not explicitly declared, int32 is used. Programming Languages generally provide syntax to declare an enumeration consisting of a group of named constants and their values.

Enum provides methods to compare such instances, convert the Instance value to its string representation, convert the numeric string representation to the instance method of this class, and create the specified enumerated Value.

You can also regard enumeration as a bit field. For more information, see flagsattribute.

Implemented Interface

This class inherits from valuetype and implements icomparable, iformattable, and iconvertible interfaces. Use the convert class for conversion, instead of using the explicit interface member of this class iconvertible.

Guidelines for flagsattribute and enum
  • Flagsattribute is used for enumeration only when bitwise operations (and, Or, XOR) are performed on values.

  • Use the power of 2 (1, 2, 4, 8, etc.) to define enumeration constants. This means that each flag in the enumerated constant of the combination does not overlap.

  • Consider creating an enumerated constant for a combination of common signs. For example, if an enumeration for file I/O operations contains the enumerated constants READ = 1 and write = 2, consider creating the enumerated constants readwrite = read or write, this enumerated constant combines the Read and Write flags. In addition, in some cases, bitwise OR operations used to combine a flag may be considered as an advanced concept, which is not required in simple tasks.

  • Exercise caution when defining a negative number as a flag enumeration constant because many flag locations may be set to 1, which may confuse your code and cause code errors easily.

  • To test whether a flag has been set in a value, perform the bitwise "and" operation between the value and the enumerated constant, in this method, all bits in the value that do not correspond to the flag are set to zero, and then the result of this operation is equal to the enumerated constant of the flag.

  • Use none as the name of the enumerated constant whose value is zero. In the bitwise AND operation, you cannot use the none enumerated constant test mark because the result is always zero. However, you can perform a logical (not bitwise) Comparison between the value and the none enumerated constant to determine whether any bits have been set in the value.

    If you create a value enumeration instead of a flag enumeration, it is still very useful to create a none enumeration constant. The reason is that by default, the memory used for enumeration is initialized to zero when the common language is run. Therefore, if a constant with a zero value is not defined, enumeration will contain an invalid value at creation.

    If there is a default value that the application needs to represent, use an enumeration constant with a zero value to represent the default value. If no default condition exists, consider using an enumeration constant whose value is zero (which means that this condition is not represented by any other enumeration constant ).

  • Do not define enumeration values that only map the enumeration status. For example, do not define enumeration constants that only mark the end of enumeration. If you need to determine the last value of the enumeration, explicitly check the value. In addition, if all values in the enumerated constant range are valid, you can also perform a range check on the first and last enumerated constants.

  • Do not specify the enumerated constants reserved for future use.

  • When defining a method or attribute that uses an enumerated constant as a value, you should consider verifying the value. The reason is that even if the value is not defined in the enumeration, it can be forcibly converted to the enumeration type.

Example

The following example shows how to use one enumeration to represent a named value and another enumeration to represent a named bit field.

C # using system;
Public class enumtest {
Enum days {Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
Enum boilingpoints {Celcius = 100, Fahrenheit = 212 };
[Flagsattribute]
Enum colors {Red = 1, Green = 2, Blue = 4, yellow = 8 };

Public static void main (){

Type weekdays = typeof (days );
Type boiling = typeof (boilingpoints );

Console. writeline ("the days of the week, and their corresponding values in the days Enum are :");

Foreach (string s in Enum. getnames (Weekdays ))
Console. writeline ("{0,-11 }={ 1}", S, enum. Format (weekdays, enum. parse (weekdays, S), "D "));

Console. writeline ();
Console. writeline ("enums can also be created which have values that represent some meaningful amount .");
Console. writeline ("The boilingpoints Enum defines the following items, and corresponding values :");

Foreach (string s in Enum. getnames (boiling ))
Console. writeline ("{0,-11 }={ 1}", S, enum. Format (boiling, enum. parse (boiling, S), "D "));

Colors mycolors = colors. Red | colors. Blue | colors. Yellow;
Console. writeline ();
Console. writeline ("mycolors holds a combination of colors. Namely: {0}", mycolors );
}
}

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.