先定义一个枚举类型

public enum PropertyType { 小学 = , 初中, 高中,大学 };

int ->enum

int d=2;

PropertyType  a=(PropertyType)d;

int <- enum

PropertyType  d = PropertyType.小学;

int a = Convert.ToInt32(d);

Enum类有关的方法

Enum.Parse 方法 (Type, String)

将一个或多个枚举常数的名称或数字值的字符串表示转换成等效的枚举对象。


public static Object Parse(
Type enumType,
string value
) 参数
enumType类型:System.Type
枚举类型。

value类型:System.String
包含要转换的值或名称的字符串。 返回值
类型:System.Object
enumType 类型的对象,其值由 value 表示。
 

如果我们有两个enum,

1 public enum Colors {Red, Green, Blue}
2  
3 public enum BgColors {Red, Black, Green, Blue }

看起來差不多。

有一天有需要把一个变成另一个來用。可能会写成这样:

1 Colors font_color = Colors.Blue;
2 BgColor bg = (BgColor)font_color;

看起來可以,编译也对。但实际上是不對的,因为实际上是转成enum所代表的int,对应的結果往往不是我们想要的。

正确的作法是

1 Colors font_color = Colors.Blue;
2 (BgColor)Enum.Parse(typeof(BgColor), font_color.ToString());

这是一种很简单的理念,但常常写成上面的写法。但日后如果其中之一有变,造成顺序有更改的話,就会出现错误。

Enum.GetName 方法

指定枚举中检索具有指定值的常数的名称。

public static string GetName(
Type enumType,
Object value
) enumType
类型:System.Type
枚举类型。 value
类型:System.Object
特定枚举常数的值(根据其基础类型)。 返回值
类型:System.String
一个字符串,其中包含 enumType 中值为 value 的枚举常数的名称;如果没有找到这样的常数,则为 null。

Enum.GetNames 方法

检索指定枚举中常数名称的数组。

public static string[] GetNames(
Type enumType
) 参数
enumType
类型:System.Type
枚举类型。 返回值
类型:System.String[]
enumType 的常数名称的字符串数组。

C#中枚举类型和int类型的转化的更多相关文章

  1. C#枚举类型和int类型相互转换

    C#枚举类型和int类型相互转换 C# 枚举(Enum) 枚举是一组命名整型常量.枚举类型是使用 enum 关键字声明的. C# 枚举是值数据类型.换句话说,枚举包含自己的值,且不能继承或传递继承. ...

  2. mysql中Numeric类型和int类型的区别

    首先记一下,Numeric数字数据只包含 数字.数字数据包括正数.负数.小数.分数和整数 例子如下: Numeric(6,2) Numeric(16,6) Numeric(16,0) 从左到右,第一个 ...

  3. 关于 Go 中 Map 类型和 Slice 类型的传递

    关于 Go 中 Map 类型和 Slice 类型的传递 Map 类型 先看例子 m1: func main() { m := make(map[int]int) mdMap(m) fmt.Printl ...

  4. Python3.x中bytes类型和str类型深入分析

    Python 3最重要的新特性之一是对字符串和二进制数据流做了明确的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Python 3不会以任意隐式的方式混用str和b ...

  5. Timestame类型和String 类型的转化

    Timestame类型和String 类型的转化 String转化为Timestamp: SimpleDateFormat df = new SimpleDateFormat("yyyy-M ...

  6. java中XMLGregorianCalendar类型和Date类型之间的相互转换

    import java.text.SimpleDateFormat;import java.util.Date;import java.util.GregorianCalendar;import ja ...

  7. js中Boolean类型和Number类型的一些常见方法

    Boolean类型 Boolean类型重写了valueOf() 方法, 返回基本布尔类型值true或false,重写了toString() 方法,返回基本字符串"true" 和 & ...

  8. char类型和int类型之间的转换

    在视屏课程第二章里,我们已经学习了一些常用的数据类型转换.然而,有一些时候我们会经常会遇到将char类型转换成int类型,或者需要将int类型转换为char类型的情况. 这里,我们来探讨一下这种不常用 ...

  9. string类型和int类型之间的转换

    一.string转int 1. 使用string流 /* 字符串转整型 */ /* * istringstream:从 string 读取数据 * ostringstream:向 string 写入数 ...

随机推荐

  1. codevs 1689 搭建高塔

    /*机智sort二维转一维*/ #include<iostream> #include<cstdio> #include<cstring> #include< ...

  2. C#解leetcode 152. Maximum Product Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  3. BestCoder 1st Anniversary

    Souvenir  Accepts: 1078  Submissions: 2366  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 26 ...

  4. JavaScript细节整理

    JavaScript是一个绝冠全球的编程语言,可用于Web开发.移动应用开发(PhoneGap.Appcelerator).服务器端开发(Node.js和Wakanda)等等.JavaScript还是 ...

  5. 安装php时,make步骤报错make: *** [ext/gd/gd.lo] Error 1

    安装PHP时,make步骤报错make: *** [ext/gd/gd.lo] Error 1 /usr/local/src/LAMP+memcahed+catci/php-5.4.0/ext/gd/ ...

  6. `~!$^*()[]{}\|;:'",<>/?在英文怎么读?

    `~!$^*()[]{}\|;:'",<>/?在英文怎么读? 'exclam'='!' 'at'='@' 'numbersign'='#' 'dollar'='$' 'perce ...

  7. DOS命令行中用MAVEN构建 Java 和 Java Web 项目

    一.Maven的安装 : 1.将 maven3.0解压到任一目录下,如D:\Program Files 2.配置环境变量:变量名: MAVEN_HOME   变量值:D:\Program Files\ ...

  8. 给出2n+1个数,其中有2n个数出现过两次,如何用最简便的方法找出里面只出现了一次的那个数(转载)

    有2n+1个数,其中有2n个数出现过两次,找出其中只出现一次的数 例如这样一组数3,3,1,2,4,2,5,5,4,其中只有1出现了1次,其他都是出现了2次,如何找出其中的1? 最简便的方法是使用异或 ...

  9. EF学习系列

    http://www.cnblogs.com/Wayou/archive/2012/09/20/EF_CodeFirst.html http://kb.cnblogs.com/zt/ef/#

  10. C题

    C - C Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description   Ass ...