C # Enumeration type conversion string enum to String

Source: Internet
Author: User

C # Enumeration type conversion string enum to String

Enumeration types are value types.

System.Enum is an abstract class (abstract class) in which all enumerated types inherit directly from it and, of course, all of its members.

All value types are descendants of System.ValueType, enumeration types are no exception, enum types inherit directly from System.Enum, and System.Enum directly inherit from System.ValueType, so the enumeration type is also The offspring of System.ValueType.
Value types are descendants of System.ValueType, but System.ValueType descendants are not all value types, System.Enum is the only special case!

All descendants of System.ValueType, except System.Enum, are value types.

In fact, we can find the System.Enum declaration in the source code of. NET:

Public abstract class Enum:valuetype, IComparable, IFormattable, iconvertible

1. All enumerated types (enum type) are value types.
2. System.Enum and System.ValueType are reference types themselves.
3. Enum types (enum type) are implicitly inherited directly from System.Enum, and this inheritance relationship can only be automatically expanded by the compiler. However, the System.Enum itself is not an enumeration type (enum type).
4. System.Enum is a special case, it inherits from System.ValueType directly, but itself is a reference type.
5. Enumeration types can be boxed into System.Enum, System.ValueType, System.Object or system.iconvertible, System.IFormattable, System.IComparable.

Note: on. NET 1.1, enum types can only be boxed to System.Enum, System.ValueType, System.Object, and on. NET 2.0, Enumeration types can also be boxed into the three interfaces implemented by System.Enum: System.iconvertible, System.IComparable, system.iformattable. The corresponding boxing operation can be either implicit or explicit


 

Enumtype

{

A=1,

b=2,

C=3

}

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

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
}

Look again

public void Enumtest ()
{
TimeOfDay time = Timeofday.afternoon;
To print an enumerated string, that is, enum type conversions (parsing) into string types
Console.WriteLine (Time.tostring ());//output: Afternoon

Gets the value of the enumeration string, which is a string representing the enumeration member, resolved to the corresponding enumeration type
TimeOfDay time2 = (timeofday) enum.parse (typeof (TimeOfDay), "Evening", true);

Enum types can be cast to an integral type, and an integral type can also be cast to an enumerated type
Console.WriteLine ((int) time2);//output: 2
Iterate through all the enumerated elements;
foreach (String s in Enum.getnames (typeof (TimeOfDay)))
{
Console.WriteLine (s);
}
Output: Morning
Afternoon
Evening
}

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.