【转载】#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 ...
随机推荐
- 【研究】Joomla二阶注入
受影响Joomla版本:3.7.0 到 3.8.3 1.下载安装Joomla3.8.3,登录后台管理系统:http://127.0.0.1/joomla/administrator/index.php ...
- (转)数位dp
原博客 https://blog.csdn.net/wust_zzwh/article/details/52100392 建议原博客看到hdu 不要62,然后看我分割线后两道题,然后再回来看原博.-- ...
- 如何在64位WIN7下安装64位的解压版mysql-5.6.37-winx64.zip
1.到mysql官网下载 https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.37-winx64.zip 2.将解压缩后的文件放到自己想要的地方, ...
- 企业DevOps构建 (一)
一,环境: tomcat 7.0.92 jenkins 1.658 maven mysql 5.5.23 mongodb 2.6.11 redis 4.0.12 01, 安装jenkins wge ...
- Json化数据-调微信接口
// 先获取用户openid列表 List<String> openids = wxPhotoUpload.getUserOpenIdList(access_token); TreeMap ...
- java连接redis使用jedis带密码
一.引入jedis的Maven配置文件 <!-- redis连接客户端jedis --> <dependency> <groupId>redis.clients&l ...
- JS正则表达式一些基本使用、验证、匹配、正则匹配时一个变量
js验证首位必须是字母 var str = "asfg"; /^[a-zA-Z].*/.test(str);//true是,false否 匹配所有空格 var str=" ...
- SpringBoot | 第三十五章:Mybatis的集成和使用
前言 最近收到公众号留言说,单纯的Mybatis的集成和使用.前面在第九章:Mybatis-plus的集成和使用介绍了基于mybatis-plus的集成和使用.后者也只是对mybatis进行了功能增强 ...
- 简单的Extjs中的Combox选择下拉框使用
{ xtype: "combobox", editable: false, emptyText: "--请选择--", mode: 'local', store ...
- python学习(七)--豆瓣爬取电影名,评分以及演员
import requestsimport re #爬取豆瓣电影排名pageNum = int(input("要查看第几页电影分数:"))#已知豆瓣默认每页展示20条#url= & ...