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. Mercurial stuck “waiting for lock”, tortoisehg pull版本卡住在等待 解决办法

    最近使用hg的时候,拖取版本一直卡住不动.报错类似waiting for lock on working directory of xxxx held by ''. 原本以为是网络不好或者hg安装有问 ...

  2. easyUI--datagrid 实现按键控制( enter tab 方向键 )

    1.表格定义时加上 onClickCell: onClickCell,2.定义列时加入编辑器3.引入 key.js 即可使用 enter 键 或者向下箭头 选中单元格下移 选中单元格上移 tab键 选 ...

  3. 面向切面编程 (AOP )

    什么是面向切面编程? 面向切面编程就是(AOP --- aspect-oriented programming), 在百科上说: 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一 ...

  4. jquery:字符串(string)转json

    第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + ...

  5. PLSQL Developer 客户端工具的安装

    安装之前先把客户端工具instantclient_12_1拷贝到一个没有中文和空格的目录中去, 比如我直接放到了D:\tools下面.注意这里需要的是32bit的. 下面开始安装PLSQL Devel ...

  6. 【程序员技术练级】学习一门脚本语言 python(一)文件处理

    现在工作上主要用的语言是java,java在企业级的应用上能够发挥很好的用途,但有时候要做一个小功能时,比如批量更新文件,抓取网页等,这时候用java就显得太笨重了.因此就学习了python这门脚本语 ...

  7. 重入锁--ReentrantLock

    本部分主要参考<java并发编程艺术>一书相关内容,同时参考https://blog.csdn.net/zhilinboke/article/details/83104597,说的非常形象 ...

  8. 移动平台的meta标签(转)

    1.Meta 之 viewport 说到移动平台meta标签,那就不得不说一下viewport了,那么什么是viewport呢? viewport即可视区域,对于桌面浏览器而言,viewport指的就 ...

  9. Android模拟器访问本机服务器

    Android模拟器访问本机服务器,用127.0.0.1访问不到,因为127.0.0.1已经被映射到模拟器了. 可以用以下两种方式访问 1. 用 10.0.2.2 2. 直接用 本机的IP地址,如:1 ...

  10. UML建模概述

    UML的组成主要有事物.图.关系. UML中的事物: (1)构件事物:UML模型的静态部分,描述概念或物理元素,包括以下 a. 类:类是对一组具有相同属性.相同操作.相同关系和相同语义的对象的抽象.包 ...