【转载】#458 Errors While Converting Between enum and Underlying Type
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的更多相关文章
- 【转载】#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 ...
- 转载 SharePoint【Site Definition 系列】– 创建Content Type
转载原地址: http://www.cnblogs.com/wsdj-ITtech/archive/2012/09/01/2470274.html Sharepoint本身就是一个丰富的大容器,里面 ...
- 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 ...
- 关于C#你应该知道的2000件事
原文 关于C#你应该知道的2000件事 下面列出了迄今为止你应该了解的关于C#博客的2000件事的所有帖子. 帖子总数= 1,219 大会 #11 -检查IL使用程序Ildasm.exe d #179 ...
- 【转载】Python: Enum枚举的实现
转自:http://www.cnblogs.com/codingmylife/archive/2013/05/31/3110656.html 从C系语言过来用Python,好不容易适应了写代码不打 ...
- [Chromium文档转载,第002章]Mojo C++ Bindings API
Mojo C++ Bindings API This document is a subset of the Mojo documentation. Contents Overview Getting ...
- C++11的enum class & enum struct和enum
C++11的enum class & enum struct和enum C++标准文档--n2347(学习笔记) 链接:http://www.open-std.org/jtc1/sc22/wg ...
- 【转】C++11的enum class & enum struct和enum
转自:https://blog.csdn.net/sanoseiichirou/article/details/50180533 C++标准文档——n2347(学习笔记) 链接:http://www. ...
- 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 ...
随机推荐
- Django ORM常用字段和参数
常用字段 AutoField int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列. IntegerField 一个整数类型,范围 ...
- C. Nice Garland-------字符串
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- nginx+uwsgi+virtualenv+supervisor部署项目
一.导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收请求,处理请求,响应请求) 基于wsgi运行的 ...
- ngin日志格式
日志格式 为了更好满足分析场景,推荐采用如下log_format配置. log_format main '$remote_addr - $remote_user [$time_local] &qu ...
- phantomJs原理
引用文段:链接:https://www.jianshu.com/p/0254391918f7 网页渲染可分为服务端渲染和客户端渲染,前者是指你在浏览器地址栏输入一个网址,Web服务器处理请求过程就将所 ...
- AWS and OpenStack
AWS OpenStack EC2 Nova EBS Cinder EFS Manila S3 Swift Storage Gateway 本地上云 ClondFront 内容发布服务 VPC Neu ...
- TimesTen启动停止命令
ttDaemonAdmin –start 启动 ttDaemonAdmin –stop 停止 或打开服务cmd-serviers.msc,找到相关服务启动或停止.
- hibernateAPI详解
1 Configuration package www.test.b_api; import org.hibernate.Session; import org.hibernate.SessionFa ...
- Servlet3.0的文件上传功能
在Servlet3.0之前,文件上传需要借助于第三方插件,在Servlet3.0之后,Servlet本身开始支持文件上传功能. 获取上传的文件可以通过HTTPServletRequest的getPar ...
- 为什么一段时间后网站后台自动退出 php中session过期时间设置
修改php配置文件中的session.gc_maxlifetime.如果想了解更多session回收机制,继续阅读.(本文环境php5.2) 概述:每一次php请求,会有1/100的概率(默认值)触发 ...