Simple use of Enum Enumeration type in C #

Source: Internet
Author: User

Enumeration types:

The enumerated type is a set of data structures composed of a group of specific constants. It is a special form of value type. When a data type consisting of a specified set of constants is required, use the enumeration type. Enumeration declaration can explicitly declare the byte, sbyte, short, ushort, int, uint, long, or ulong types as the corresponding basic types. The enumeration declaration without explicitly declaring the basic type means that the corresponding basic type is int.

 

Note:

1. For an enumeration type without a value assignment, the declared first enumeration member has a zero Mo value. The following enumerated Member values are obtained by adding 1 to the value of the previous enumerated member (in the text order.

2. Multiple enumerated members can have the same value. The value of the enumerated member is not displayed. The value of the previous enumerated member is always + 1.

3. Pay attention to type conversion during use.

We can see that the enumeration type is essentially a number. If you need to map the enumerated values to their corresponding strings during display, the following is a simple solution.

 

[Csharp] public enum DataTypeId
{
[StringValue ("Money")]
Money = 0,
[StringValue ("Number")]
Number = 1,
[StringValue ("Datetime")]
Datetime = 2,
[StringValue ("LongText")]
LongText = 3,
[StringValue ("plain text")]
Required text = 4,
[StringValue ("IdeaType")]
IdeaType = 5,
[StringValue ("Status")]
Status = 6
}
 
Public class StringValue: System. Attribute
{
Private string _ value;
 
Public StringValue (string value)
{
_ Value = value;
}
 
Public string Value
{
Get {return _ value ;}
}
 
}
 
Public static class StringEnum
{
Public static string GetStringValue (Enum value)
{
String output = null;
Type type = value. GetType ();
 
FieldInfo fi = type. GetField (value. ToString ());
StringValue [] attrs =
Fi. GetCustomAttributes (typeof (StringValue ),
False) as StringValue [];
If (attrs. Length> 0)
{
Output = attrs [0]. Value;
}
 
Return output;
}
}

Public enum DataTypeId
{
[StringValue ("Money")]
Money = 0,
[StringValue ("Number")]
Number = 1,
[StringValue ("Datetime")]
Datetime = 2,
[StringValue ("LongText")]
LongText = 3,
[StringValue ("plain text")]
Required text = 4,
[StringValue ("IdeaType")]
IdeaType = 5,
[StringValue ("Status")]
Status = 6
}

Public class StringValue: System. Attribute
{
Private string _ value;

Public StringValue (string value)
{
_ Value = value;
}

Public string Value
{
Get {return _ value ;}
}

}

Public static class StringEnum
{
Public static string GetStringValue (Enum value)
{
String output = null;
Type type = value. GetType ();

FieldInfo fi = type. GetField (value. ToString ());
StringValue [] attrs =
Fi. GetCustomAttributes (typeof (StringValue ),
False) as StringValue [];
If (attrs. Length> 0)
{
Output = attrs [0]. Value;
}

Return output;
}
}


When using StringEnum, the string output by StringEnum. GetStringValue (DataTypeID. Money) is the string corresponding to the MyDataTypeId enumerated value.

 


There is also a simple way to get the string corresponding to the enumerated value. DataTypeID. Money. ToString () returns the Money string. When the Web page is displayed, DataTypeID. Money displays 0, while DataTypeID. Money. ToString () returns the Money string.


 

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.