Enum 提供一些实用的静态方法:
    (1)比较枚举类的实例的方法
    (2)将实例的值转换为其字符串表示形式的方法
    (3)将数字的字符串表示形式转换为此类的实例的方法
    (4)创建指定枚举和值的实例的方法。

举例:enum Colors { Red, Green, Blue, Yellow };

Enum-->String

(1)利用Object.ToString()方法:如Colors.Green.ToString()的值是"Green"字符串;

(2)利用Enum的静态方法GetName与GetNames:

public static string GetName(Type enumType,Object value)

public static string[] GetNames(Type enumType)

例如:Enum.GetName(typeof(Colors),3))与Enum.GetName(typeof(Colors), Colors.Blue))的值都是"Blue"

Enum.GetNames(typeof(Colors))将返回枚举字符串数组。

String-->Enum

(1)利用Enum的静态方法Parse:

public static Object Parse(Type enumType,string value)

例如:(Colors)Enum.Parse(typeof(Colors), "Red")

Enum-->Int

(1)因为枚举的基类型是除 Char 外的整型,所以可以进行强制转换。

例如:(int)Colors.Red, (byte)Colors.Green

Int-->Enum

(1)可以强制转换将整型转换成枚举类型。

例如:Colors color = (Colors)2 ,那么color即为Colors.Blue

(2)利用Enum的静态方法ToObject。

public static Object ToObject(Type enumType,int value)

例如:Colors color = (Colors)Enum.ToObject(typeof(Colors), 2),那么color即为Colors.Blue

判断某个整型是否定义在枚举中的方法:Enum.IsDefined

public static bool IsDefined(Type enumType,Object value)

例如:Enum.IsDefined(typeof(Colors), n))

C# Enum,Int,String的互相转换相关推荐

  1. (转)C# Enum,Int,String的互相转换 枚举转换

    (转)C# Enum,Int,String的互相转换 枚举转换 Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来 ...

  2. C# Enum,Int,String的互相转换 枚举转换

    Enum为枚举提供基类,其基础类型可以是除 Char 外的任何整型.如果没有显式声明基础类型,则使用 Int32.编程语言通常提供语法来声明由一组已命名的常数和它们的值组成的枚举.注意:枚举类型的基类 ...

  3. python3 byte int string 互转 转换

    # bytes 与 int b=b'\x01\x02' num=int.from_bytes(b,'little') print('bytes转int:',num)b1=num.to_bytes(2, ...

  4. enum与int、String之间的转换

    enum与int.String之间的转换 enum<->int enum -> int: int i = enumType.value.ordinal(); int -> en ...

  5. CString,int,string,char*之间的转换(转)

    ............................................................... <C++标准函数库>中说的   有三个函数可以将字符串的内容 ...

  6. integer 转int类型 java_Java数据类型中String、Integer、int相互间的转换

    Java数据类型String.Integer.int相互间的转换 1.Integer转换成int的方法 Integer i = new Integer(10); int k = i.intValue( ...

  7. Character,String相关方法,Int,double互相转换

    /** * String相关方法* */ public class StringTest2 {/*** @param args*/public static void main(String[] ar ...

  8. VC之CString,wchar_t,int,string,char*之间的转换

    VC之CString,wchar_t,int,string,char*之间的转换 1. CString 转 wchar_t CString path = "asdf"; wchar ...

  9. int和String类型的转换

    int和String类型的转换 1. int转换为String类型 public static String valueOf(int i)返回int参数的字符串字符串表示形式.该方法是String类中 ...

最新文章

  1. javascript 判断 前端 是 pc端 还是 移动端
  2. 报错解决:error: this statement may fall through [-Werror=implicit-fallthrough=]
  3. 豆瓣FLASHMP3播放器
  4. C++ STL库的总结以及实现原理
  5. mfc怎么获取进程的线程数_Python多线程获取小米应用商店App,看看我是怎么做到的
  6. linux 重定向 不换行,Ada:重定向到stdout时省略换行符(测试Put)
  7. php不常用,却很实用的6个字符串函数
  8. Linux常用命令(补充)--其他
  9. P3399 丝绸之路(线性二维dp)
  10. ssh mysql视屏_ssh+mysql实现的Java web在线订电影票系统项目源码附带视频指导教程...
  11. 浏览器的NPAPI插件技术不要学了,已经淘汰几年了
  12. txtv28pw河南某中学_有一种寒冷叫不穿秋裤!河南一中学班主任让学生列队挨个检查秋裤...
  13. 关于flutter出现Because flutter_app depends on flutter_screenutil >=3.0.0-beta.1 which requires Flutter S
  14. Python数据可视化:Cartopy 地理空间数据可视化
  15. IPv6地址的无状态自动配置
  16. 漏洞原理——ssrf
  17. ADI中文手册获取方法
  18. 嵌入式单片机应用开发--FLASH的擦除操作(擦除开始地址到结束地址内的数据)
  19. 【vue】前端根据后端返回的code码查字典表展示文本
  20. TMS320C54x硬件结构及原理

热门文章

  1. freemarker该阵列
  2. IT人士运动方式选择建议
  3. linux学习文档-1
  4. hdu-1576(A/B)
  5. 经典算法——KMP模式匹配
  6. 返回一个二维整数数组中最大子数组的和
  7. Linux之Redis安装
  8. 关于VS AddIn的注册
  9. 【转】 MySQL索引类型一览 让MySQL高效运行起来 mysql索引注意事项
  10. NFS问题:clnt_create: RPC: Port mapper failure - Unable to receive: errno 113 (No route to host)