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. server 2012 R2查询端口

    1. win+r弹出运行对话框,输入cmd,打开cmd窗口 netstat -ano | findstr "80" (注80是你想要看查看的端口号) 就会输出包含80端口使用的情况 ...

  2. CAPL编程实现诊断刷写,车联网FOTA流程自动化测试(代码篇)

    原创内容,转载请注明出处   接上篇,本文主要讲CAPL编程详细实现,软件环境CANoe  11.0 一.Simulation Setup 1.建模之前,首先创建一个.DBC文件.如果不会,可以用一个 ...

  3. Blockly

    Blockly简介 A library for building visual programming editors.  Blockly 是个库,可用来构建可视化编程编辑器 Blockly is b ...

  4. 基于云计算的IaaS、PaaS、SaaS三种服务模式的区别

    Infrastructure-as-a-Service(IaaS) - 基础即设施服务 基础设施主要包括网络系统(networking).存储设备(storage).服务器(servers).虚拟化技 ...

  5. 树莓派开启wlan功能

    烧好系统之后,通过网线连接树莓派到路由器.通过ip登入系统,修改interfaces文件,添加下面内容 sudo nano /etc/network/interfacesauto wlan0allow ...

  6. 踩坑--springboot打war包

    springboot需要jdk1.8+tomcat8.0 第一步:从IDEA中通过maven install将项目打成war包 第二步:将war包放在tomcat的webApp目录下,启动tomcat ...

  7. Linux的应用层到底层驱动的调用过程

    应用层如何内核.md 1.从应用层打通内核:驱动 首先来说是设备号的引入,我们通过 cat/proc/kallsyms |grep mydevice 可以查看设备号,当然我们也是可以自己创建设备号,这 ...

  8. repoquery详解——linux查看包依赖关系的神器

    repoquery是yum扩展工具包yum-utils中的一个工具,所有如果你没有repoquery命令的话,可以先 sudo yum install yum-utils 安装yum-utils包.是 ...

  9. pat02-线性结构1. Reversing Linked List (25)

    02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...

  10. mongoDB cpu飙高问题

    问题描述: 最近几天生产环境上的mongodb一直在报警,cpu飙高,其他如内存.iops.连接数.磁盘操作等都正常.通过定位业务,发现是由于mongodb的表其中一个查询未建立索引导致,110多W的 ...