"You must know. NET"-Simplicity: Understanding Enumerations (Ⅲ)

Source: Internet
Author: User

The main contents of this section include:

Enumeration type full solution;

bit marking applications;

Enumerates the application rules.

Ⅰ, enumerating

  Where can I see the enumeration? Open the properties of each file, we will see a read-only, hidden options, when you manipulate a file, you can use a read-only, writable, append and other modes.

In. NET you can choose to express this simple classification structure in static fields of the class, but the wiser choice is obviously--enumeration!

All enum types are implicitly and implicitly inherited from the System.Enum type, the System.Enum type is a reference type, and the only reference type that inherits from System.ValueType is not a value type.

An enumeration type is a value type that is assigned to the thread's stack and automatically inherits from the enum type, but cannot be inherited by itself; The enum type is a reference type and is assigned to the managed heap, and the enum itself is not an enumeration type, but it provides a common method for manipulating enumeration types.

        Public enum Week:int        {            Sun = 7,            Mon = 1,            Tue, Wed, Thur, Fri, Sat,            weekend = Sun        }
A. Type conversions: Because enum types are essentially collections of certificate types, you can type conversions with integer types to each other, but must be explicit.
            int i = (int) Week.fri;            Week J = (week) 3;

Or use the Parse method to do it indirectly. Week W = (week) Enum.parse (typeof (Week), "2");

B. Mapping to a string:

Includes the ToString instance method and the parse static method.

C. Reciprocal conversions of different enumerations: display transformations, such as

Otherweek i = Otherweek.mon;

Week today = (week) I;

D. Other reference type conversions:

Enum inherits from--valuetype,icomparable,iformattable,iconvertible.

Therefore, you can explicitly convert the above types, and System.Object, System.Enum.

E. Common methods:

GetNames, gets an array of symbol names in the enumeration;

GetValues, gets all the symbol arrays in the enumeration;

IsDefined, determines whether the symbol or integer exists in the enumeration;

A getunderlyingtype that returns the declaration type of the enumeration instance.
    

Ⅱ, Bit enumeration

  An enumeration marked with the [Flags] attribute. The purpose of the FlagsAttribute attribute is to treat an enumeration member as a bit token, rather than as an orphaned constant.  

The a.enum.isdefined method cannot handle a bit enumeration member, which can be used with the enumeration member for a bitwise AND operation, and a result of not 0 indicates that the variable contains the enumeration member, as
if ((Test & color.red)!=0)
The B.flags feature affects the execution process and results of the ToString, parse, and format methods.

  

  

"You must know. NET"-Simplicity: Understanding Enumerations (Ⅲ)

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.