Research on enumeration in C # (skip some small pitfalls of net ),
Previously, I used Enum. Parse () to convert the string into enumeration. I did not go into detail. I found a problem later, and I had a preliminary study on the Enum below (. net 4.0 ).
Enumeration is a specified constant set. Its basic type can be any integer except Char. If the basic type is not explicitly declared, Int32 is used. Enum is the base class of all enumeration in. NET.
Never create an enumeration type with a non-integer basic type. Although this enumeration type can be created by using reflection, the method call of the obtained type is unreliable and may cause other exceptions.
See the following code.
(Note that the enumeration type is a value type, and its value cannot be Null. Therefore, we define an enumeration type. Its default value is usually the first element of the enumeration or an element with a value of 0)
First, define a test enumeration.
Enum test {aaa, bbb, ccc, ddd}
(1) Compare the Parse method now (note that the value parameter in the parse method is case-sensitive. To ignore the case, you must use the override method with the ignoreCase parameter) private void button#click (object sender, EventArgs e ){
Test te;
Te = (test) Enum. Parse (typeof (test), "4"); // 4
TypeStr = te. GetType (). Name; // test
Te = (test) Enum. Parse (typeof (test), "2"); // ccc
TypeStr = te. GetType (). Name; // test
Te = (test) Enum. Parse (typeof (test), "ddd"); // ddd
Te = (test) Enum. Parse (typeof (test), "ddd1"); // System. ArgumentException ("the request value" ddd1 "is not found ". ")
}
What about the TryParse method?
TestTe = test. bbb;
//(Note: The TryParse method is assigned the default value without initialization. If you do not want the TryParse method to change the Out enumerated value, call the IsDefined () (IsDefined is case sensitive to strings) method first.
// Make sure that the specific string representation of the integer is actually a member of TEnum .)
if (Enum.TryParse("aaa", out te)) //aaa
MessageBox.Show(te.GetType().Name); //test
if (Enum.TryParse("111", out te)) //111
MessageBox.Show(te.GetType().Name);
TryParse method definition
public static bool TryParse<TEnum>( string value, out TEnum result)where TEnum : struct
The TryParse method is a bit strange. As long as the value can be converted to a number, it will succeed. If not, it will be converted based on the case (case sensitive by default). If it does not exist, false will be returned.
Conclusion:Enum. Parse ()If you want to use the TryParse () method to determine the returned bool value, we recommend that you first use Enum. IsDefined () to determine whether the returned bool value exists.
(2) Enum. ToObject () method
Te = (test) Enum. ToObject (typeof (test), 4); // 4
As you can see,ToObject ()The same as the Parse method.ArgumentException, and the conversion is successful, soWe recommend that you first use Enum. IsDefined () to determine whether it exists.
(3)Enum. GetValues () method
This method does not obtain the array with the result {0, 1, 2, 3}. Instead, it returns the same result as the GetNames () method, except that the GetNames return value type is string [].
If you want to obtain {0, 1, 2, 3}, refer to this method.
Public static Array getValuesArray (Type enumType) {Array tempArry; tempArry = Enum. getValues (enumType); // tempArry = enumType. getEnumValues (); Array result = Array. createInstance (typeof (int), tempArry. length); int j = 0; foreach (int I in tempArry) {result. setValue (I, j ++);} return result ;}
(4) Always UseEnum. IsDefined ()Method to determine whether there is,Let's take a look at this method.
Bool B; B = Enum. isDefined (typeof (test), 3); // Trueb = Enum. isDefined (typeof (test), 4); // falseb = Enum. isDefined (typeof (test), "aaa"); // trueb = Enum. isDefined (typeof (test), "aaA"); // falseb = Enum. isDefined (typeof (test), "aaA1"); // false
(5) Now let's look at Enum. GetName (Type enumType,Object value)
How is the string and integer processed by the method?
String s; s = Enum. getName (typeof (test), 3); // ddds = Enum. getName (typeof (test), 4); // null // s = Enum. getName (typeof (test), "3"); // System. argumentException (the input value must be an enumeration base or basic type, such as Int32) // s = Enum. getName (typeof (test), "aaa"); // System. argumentException (the input value must be an enumeration base or basic type, such as Int32)
Based on the above summary, we will useEnum. IsDefined ()Method judgment,
For strings, use Parse or TryParse for conversion. because too many. net methods are common and case-insensitive, remember to useOverride method with ignoreCase parameters.
You can useParse andToObject method.When you convert an integer to an enumeration value, you can assign a value that is not actually an enumeration member. To prevent this problem, you can pass the integer to the IsDefined () method before performing the conversion.
Convert enumeration to a string or integer value. You can call the static Format method and reload the instance ToString () method. Or GEtName ().
The Enum class provides an explicit interface of the IConvertible interface to Convert from the enumerated value to the integer type. Therefore, you can use Convert. ToInt32.
Convert.ToInt32(test.aaa);
If you want to convert an enumerated element or value to a string (integer ),Enum. GetValuesUse (int) to convert.