C#中enum与string的相互转换的示例


当前第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;


1

ty.ToString();

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》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...