从String到enum的互换(string to enum to string)
2006-07-02 20:39
197 查看
Convert a string to an enumerated (enum) value.
Using the Enum.Parse method, you can easily convert a string value to an enumerated value. Doing this requires the type of the enum and string value. Adding the true argument will cause the case to be ignored.
Using the following enum for this example:
private enum Aircraft
{
Beech,
Cessna,
Piper
}
You can easily convert the string to an enum value like this:
Aircraft air = (Aircraft) Enum.Parse(typeof(Aircraft), "Cessna", true);
Ideally you should wrap a try-catch around the Enum.Parse statement.
string s;
s = air .ToString();
s = Enum.GetName(typeof(Aircraft), air.Beech);
后者比前者性能高
Using the Enum.Parse method, you can easily convert a string value to an enumerated value. Doing this requires the type of the enum and string value. Adding the true argument will cause the case to be ignored.
Using the following enum for this example:
private enum Aircraft
{
Beech,
Cessna,
Piper
}
You can easily convert the string to an enum value like this:
Aircraft air = (Aircraft) Enum.Parse(typeof(Aircraft), "Cessna", true);
Ideally you should wrap a try-catch around the Enum.Parse statement.
string s;
s = air .ToString();
s = Enum.GetName(typeof(Aircraft), air.Beech);
后者比前者性能高
相关文章推荐
- C# 枚举类型转换字符串 Enum to string,枚举转换为下拉菜单 Enum DropDownList
- C# : Enum and overriding ToString on it
- string to enum in systemverilog
- ios enum to string
- string to enum 像函数指针那样使用的预编译
- string to enum 字符串转枚举
- Enum to String and vice versa in C++
- C# 枚举类型转换字符串 Enum to string,枚举转换为下拉菜单 Enum DropDownList
- c sharp string to Enum
- How to convert enum to string with boost library
- Convert a string to an enum
- Java – Convert String To Enum Object
- C# : Enum and overriding ToString on it
- Convert a string to an enum
- 【转】C# : Enum and overriding ToString on it
- Convert a string to an enum.
- Enum枚举序列化JSON value to string问题
- Unity String To Enum
- asp.net中System.DateTime.Now.ToString()的一些用法
- C# ToString()方法 带参数的特殊用法