C # convert Enum to string for Enumeration type conversion

Source: Internet
Author: User

C # convert enum to string for Enumeration type conversion

All enumeration types are value types.

System. enum is an abstract class. All enumeration types inherit from it directly, and of course all its members.

All value types are system. the descendant of valuetype, And the enumeration type is no exception. The Enumeration type is directly inherited from system. enum, while system. the enum is directly inherited from the system. valuetype, so the enumeration type is also system. the descendant of valuetype.
The value types are all descendants of system. valuetype, but the descendants of system. valuetype are not all value types, and system. enum is the only special case!

Among all the descendants of system. valuetype, all except system. enum are value types.

In fact, we can find the declaration of system. enum in the source code of. net:

Public abstract class enum: valuetype, icomparable, iformattable, iconvertible

1. All enumeration types (enum type) are value types.
2. system. enum and system. valuetype are reference types.
3. The enum type is implicitly directly inherited from system. enum, and the inheritance relationship can only be automatically expanded by the compiler. However, system. enum is not an enumeration type ).
4. system. enum is a special case. It inherits directly from system. valuetype, but it is a reference type.
5. enumeration types can be boxed into system. enum, system. valuetype, system. object, system. iconvertible, system. iformattable, and system. icomparable.

Note: In.. net 1.1, the enumeration type can only be boxed to system. enum, system. valuetype, system. object.. net 2.0, the enumeration type can also be boxed to system. the three interfaces implemented by enum: system. iconvertible, system. icomparable, system. iformattable. The corresponding packing operation can be either implicit or explicit.


 

Enumtype

{

A = 1,

B = 2,

C = 3

}

 

Enumtype newtype = (enumtype) enum. parse (typeof (enumtype), "");

 

String [] names = emum. getnames (typeof (enumtype ));

Array values = enum. getvalues (typeof (enumtype ));

For (int I = 0; I <values. length-1; I ++)

{

Dropdownlist. items. add (new listitem (names [I], values. getvalue (I )));

}

. Net public enum timeofday
{
Morning = 0,
Afternoon = 1,
Evening
}

Check again

Public void enumtest ()
{
Timeofday time = timeofday. afternoon;
// Print the enumerated string, that is, convert (PARSE) the enumerated type to the string type
Console. writeline (time. tostring (); // output: afternoon

// Obtain the enumerated string value, that is, a string that represents the enumerated Member, resolved to the corresponding enumerated type
Timeofday time2 = (timeofday) enum. parse (typeof (timeofday), "evening", true );

// The Enumeration type can be forcibly converted to an integer type, or an integer can be forcibly converted to an enumeration type.
Console. writeline (int) time2); // output: 2
// Traverse all enumeration elements;
Foreach (string s in enum. getnames (typeof (timeofday )))
{
Console. writeline (s );
}
// Output: morning
// Afternoon
// Evening
}

 

Related Article

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.