【转载】#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 ...
随机推荐
- MySQL 重设root密码
Mysql 5.6.15版本的windows下的重设root密码,找了n个帖子终于弄明白了. 1. 开一个cmd窗口,进入Mysql的安装目录的bin文件夹,然后运行这个: mysqld --skip ...
- 豆瓣模拟登录(双层html)
一.豆瓣模拟登录(双层html) #!/usr/bin/env python # -*- coding: utf-8 -*- #author tom import time from selenium ...
- Maven系统学习
1. 1.1 何为构建? 编译.测试.运行.打包.部署等工作: Maven就是用软件的办法让这一系列工作自动化,只需要一条简单的命令,所有繁琐的工作就会自动完成: Maven最大的消除了构建的重复,抽 ...
- 问题记录——java.lang.IllegalArgumentException: Illegal character in scheme name at index 0
以下http请求报错是因为,请求的地址前面有个空格.... 2019-01-09 03:30:23,154 ERROR [business.modules.merchantreportresult.s ...
- mysql 显示树结构表的节点全路径
SELECT TYPEID AS TYPEID, pTYPEID AS 父TYPEID, levels AS 父到子之间级数, concat(paths, ',', TYPEID) AS 父到子路径, ...
- 把linux图形启动界面修改成命令行界面
由于图形界面比较耗资源,需要把启动界面修改成命令行界面,怎么修改呢? 1.vim /etc/inittab 2.把id:5:initdefault:改成 id:3:initdefault: 3.重启即 ...
- js面向对象3
1.this的使用 核心:在js中,this表示当前对象,“谁”调用了当前函数,“this”就指向了“谁” 语法: Function 类(){ this.属性=值; } 例1.在构造器中,使用this ...
- 上下文(Context)和作用域(Scope)
函数的每次调用都有与之紧密相关的作用域和上下文.从根本上来说,作用域是基于函数的,而上下文是基于对象的. 换句话说,作用域涉及到所被调用函数中的变量访问,并且不同的调用场景是不一样的.上下文始终是th ...
- 6、图标:icon
1.图标 /* ---html----*/ <ion-content text-center class="icons-basic-page"> <ion-row ...
- 网页设计,Access入门 2010,语文
Access入门 2010(select查询) order by(按升序排列)?+desc(按降序排列) group by(进行分组,下一行只能用having) sum(数值总数)?+as?(新字段的 ...