当前第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#开发中遇到的问题分享
c#中string和string有什么区别?
介绍c#中的接口
c#中关于静态与非静态方法的区别介绍
c# 一些面试试题的实例教程
比较c#和java中面向对象语法的区别
dynamic(c# 参考)
c# invoke 和 begininvoke之间的区别详解
c#如何通过对象属性名修改值的实例
c# winform跨线程访问控件的图文详解
更多相关阅读请进入《csharp》频道 >>
清华大学出版社
作者:[美]克里斯琴·内格尔(Christian Nagel)著。出版时间:2019年3月。
转载请注明出处:木庄网络博客 » C#中enum与string的相互转换的示例