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.