You can convert to an enum value from its underlying type by casting the underlying type (e.g. int) to the enum type.

However, when you cast a value that doesn't have a corresponding enumerator in the type, you don't get any sort of error.

In the example below,the Mood type has enumerators that take on the values (0, 1, 2, 3). But we can successfully cast a value of 4 to the Mood type.

 public enum Mood { Crabby, Happy, Petulant, Elated };

 static void Main()
{
int moodValue = ;
Mood mood; mood = (Mood)moodValue;
Console.WriteLine(mood); //
}

To detect this problem, you can check to see if the value is defined in the enumerated type using the Enum.IsDefined method.

 if (Enum.IsDefined(typeof(Mood), moodValue))
{
mood = (Mood)moodValue;
}
else
{
Console.WriteLine("{0} is not a valid Mood value!", moodValue);
}

原文地址:#458 Errors While Converting Between enum and Underlying Type

【转载】#458 Errors While Converting Between enum and Underlying Type的更多相关文章

  1. 【转载】#457 Converting Between enums and their Underlying Type

    When you declare an enum, by default each enumerated value is represented internally with an int. (S ...

  2. 转载 SharePoint【Site Definition 系列】– 创建Content Type

    转载原地址:  http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...

  3. C# Convert an enum to other type of enum

    Sometimes, if we want to do convert between two enums, firstly, we may think about one way: var myGe ...

  4. 关于C#你应该知道的2000件事

    原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...

  5. 【转载】Python: Enum枚举的实现

    转自:http://www.cnblogs.com/codingmylife/archive/2013/05/31/3110656.html   从C系语言过来用Python,好不容易适应了写代码不打 ...

  6. [Chromium文档转载,第002章]Mojo C++ Bindings API

    Mojo C++ Bindings API This document is a subset of the Mojo documentation. Contents Overview Getting ...

  7. C++11的enum class & enum struct和enum

    C++11的enum class & enum struct和enum C++标准文档--n2347(学习笔记) 链接:http://www.open-std.org/jtc1/sc22/wg ...

  8. 【转】C++11的enum class & enum struct和enum

    转自:https://blog.csdn.net/sanoseiichirou/article/details/50180533 C++标准文档——n2347(学习笔记) 链接:http://www. ...

  9. Entity Framework Tutorial Basics(32):Enum Support

    Enum in Entity Framework: You can now have an Enum in Entity Framework 5.0 onwards. EF 5 should targ ...

随机推荐

  1. Java打包成jar

    若要生成一个名为 cal.jar 的可执行jar文件:(文件名可以是任意合法名字)  (这是我认为简单实用的一种方法,还有很多别的方法在此就不介绍了)  第一 把程序生成的所有字节码文件(即.clas ...

  2. 给新人看的 JavaScript的继承

    //这个继承方式是给新人看的,逻辑简单一些 Object.extend=function(props){ //继承父类 var prototype=Object.create(this.prototy ...

  3. mysql允许数据库远程连接

    2018-11-06 进入数据库 mysql -uroou(用户) -p123456(密码) 授权某个user可远程访问 grant all privileges on *.* to ' with g ...

  4. centos7.1部署java环境服务器

    1.检查操作系统自带java是jdk还是jre(否有javac,本例中没有javac) [root@bogon ~]# ls -l /usr/lib/jvm/总用量 0drwxr-xr-x. 3 ro ...

  5. PIE SDK图片元素的绘制

    1. 功能简介 在数据的处理中会用到图片元素的绘制,利用IPictureElement图片元素接口进行绘制,目前PIE SDK支持IPictureElement元素接口的绘制,下面对图片元素的绘制进行 ...

  6. Percona Mysql备份

    介绍 Percona是唯一一款开源.免费的mysql热备份工具,实现了对InnoDB数据库的非阻塞式的备份.有如下优势:1.完整.快速.可靠的备份2.备份期间不打断事务执行(innodb引擎)3.备份 ...

  7. Oracle DG --检查

    检查DG是否同步 01,  查看主备可切换状态 select switchover_status from v$database;select db_unique_name,open_mode,dat ...

  8. 修改OPENSUSE 桌面快速搜索快捷键

  9. 移除“xmlns”命名空间

    用XmlDocument创建一个文档,或者插入一个节点,默认会生成xmlns(命名空间)特性. 假定有一个xml文档如下结构: <?xml version="1.0" enc ...

  10. emacs使用笔记

    C-h t tutorial [移动基本操作]C-f C-b C-p C-n 前后上下 C-v C-a 行首 C-e行尾C-a 和 C-e 可以将光标移动到"一行"的头部和尾部.M ...