当前第2页 返回上一页
1 2 3 | Enum.GetName( typeof (Colors),3))与Enum.GetName( typeof (Colors),
Colors.Blue))的值都是 "Blue"
Enum.GetNames( typeof (Colors))将返回枚举字符串数组
|
3)、RecipientStatus ty = RecipientStatus.Delivered;
2、字符串转枚举(string->enum)
1)、利用Enum的静态方法Parse: Enum.Parse()
原型:
1 2 3 4 | public static Object Parse(Type enumType, string value)
eg : (Colors)Enum.Parse( typeof (Colors), "Red" );
(T)Enum.Parse( typeof (T),
strType)
|
一个模板函数支持任何枚举类型
1 2 3 4 5 6 7 | protected static
T GetType<T>( string strType)
{
T t = (T)Enum.Parse( typeof (T),
strType);
return t;
}
|
判断某个枚举变量是否在定义中:
1 2 3 4 | RecipientStatus type =
RecipientStatus.Sent;
Enum.IsDefined( typeof (RecipientStatus),
type );
|
总结
以上就是C#中enum与string的相互转换的示例的详细内容!
返回前面的内容
相关阅读 >>
c#中匿名对象与var以及动态类型 dynamic的详解
c#之fastsocket实战项目的示例分享
有关c#工厂模式简单讲解
c#如何生成二维码的示例分享
c#中使用反射以及特性简化的实例代码
c#实现操作字符串的方法总结
c#中方向键与回车键切换控件焦点的两种方法
分享在c#中时间戳是怎么转换的?
c#中发送邮件的实现方法详解
详解可选参数和命名参数实例
更多相关阅读请进入《csharp》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » C#中enum与string的相互转换的示例