C#中enum的总结(转载)
enum Fabric{Cotton = 1,Silk = 2,Wool = 4,Rayon = 8,Other = 128}
int fabNum = (int)fab;//转换为常数值。必须使用强制转换。
string fabVal = fab.ToString ("D");//显示常数值
{
YANGZHIPING = 1,
GUANGUIQIN = 2,
YANGHAORAN = 4,
LIWEI = 8,
GUANGUIZHI = 16,
LISIWEN = 32,
LISIHUA = 64,
}
{
Console.WriteLine(s);
}
根据下面的两个例子,粗略地说,一方面,设置标志[Flags]或者[FlagsAttribute],则表明要将符号名列举出来;另一方面,可以通过强制转换,将数字转换为符号名。说不准确。看下面的例子体会吧。注意:
Console.WriteLine("MyFabric = {0}", fab);//输出:Fabric.Cotton | Fabric.Rayon | Fabric.Silk;
例二:class FlagsAttributeDemo
{
// Define an Enum without FlagsAttribute.
enum SingleHue : short
{
Black = 0,
Red = 1,
Green = 2,
Blue = 4
};// Define an Enum with FlagsAttribute.
[FlagsAttribute]
enum MultiHue : short
{
Black = 0,
Red = 1,
Green = 2,
Blue = 4
};static void Main( )
{
Console.WriteLine(
"This example of the FlagsAttribute attribute /n" +
"generates the following output." );
Console.WriteLine(
"/nAll possible combinations of values of an /n" +
"Enum without FlagsAttribute:/n" );
// Display all possible combinations of values.
for( int val = 0; val <= 8; val++ )
Console.WriteLine( "{0,3} - {1}", val, ( (SingleHue)val ).ToString( ) );
Console.WriteLine( "/nAll possible combinations of values of an /n" + "Enum with FlagsAttribute:/n" );
// Display all possible combinations of values.
// Also display an invalid value.
for( int val = 0; val <= 8; val++ )
Console.WriteLine ( "{0,3} - {1}", val, ( (MultiHue)val ).ToString( ) );
}
}/*
This example of the FlagsAttribute attribute
generates the following output.All possible combinations of values of an
Enum without FlagsAttribute:0 - Black
1 - Red
2 - Green
3 - 3
4 - Blue
5 - 5
6 - 6
7 - 7
8 - 8All possible combinations of values of an
Enum with FlagsAttribute:0 - Black
1 - Red
2 - Green
3 - Red, Green
4 - Blue
5 - Red, Blue
6 - Green, Blue
7 - Red, Green, Blue
8 - 8
*/
public static double GetPrice(Fabric fab)
{
switch (fab)
{
case Fabric.Cotton: return (3.55);
case Fabric.Silk: return (5.65);
case Fabric.Wool: return (4.05);
case Fabric.Rayon: return (3.20);
case Fabric.Other: return (2.50);
default: return (0.0);
}
}
6、上面三点一个完整的例子
//1、enum的定义
public enum Fabric : short
{
Cotton = 1,
Silk = 2,
Wool = 3,
Rayon = 8,
Other = 128
}
//将枚举作为参数传递
public static double GetPrice(Fabric fab)
{
switch (fab)
{
case Fabric.Cotton: return (3.55);
case Fabric.Silk : return (5.65);
case Fabric.Wool: return (4.05);
case Fabric.Rayon: return (3.20);
case Fabric.Other: return (2.50);
default: return (0.0);
}
}
public static void Main()
{
Fabric fab = Fabric.Cotton;
int fabNum = (int)fab;
string fabType = fab.ToString();
string fabVal = fab.ToString ("D");
double cost = GetPrice(fab);
Console.WriteLine("fabNum = {0}/nfabType = {1}/nfabVal = {2}/n", fabNum, fabType, fabVal);
Console.WriteLine("cost = {0}", cost);
}
7、Enum类的使用
Enum.IsDefinde、Enum.Parse两种方法经常一起使用,来确定一个值或符号是否是一个枚举的成员,然后创建一个实例。Enum.GetName打印出一个成员的值;Enum.GetNames打印出所有成员的值。其中注意typeof的使用。这一点很重要。
public enum MyFamily
{
YANGZHIPING = 1,
GUANGUIQIN = 2,
YANGHAORAN = 4,
LIWEI = 8,
GUANGUIZHI = 16,
LISIWEN = 32,
LISIHUA = 64,
}
string s = "YANGHAORAN";
if (Enum.IsDefined(typeof(MyFamily), s))
{
MyFamily f = (MyFamily)Enum.Parse(typeof(MyFamily), s);
GetMyFamily(f);
Console.WriteLine("The name is:" + Enum. GetName(typeof(MyFamily), 2));
string[] sa = Enum.GetNames(typeof(MyFamily));
foreach (string ss in sa)
{
Console.WriteLine(ss);
}
}
MSDN解释:http://msdn.microsoft.com/zh-cn/library/sbbt4032%28VS.80%29.aspx
C#中enum的总结(转载)的更多相关文章
- MVC图片上传详解 IIS (安装SSL证书后) 实现 HTTP 自动跳转到 HTTPS C#中Enum用法小结 表达式目录树 “村长”教你测试用例 引用provinces.js的三级联动
MVC图片上传详解 MVC图片上传--控制器方法 新建一个控制器命名为File,定义一个Img方法 [HttpPost]public ActionResult Img(HttpPostedFile ...
- MYSQL中 ENUM 类型
MYSQL中 ENUM 类型的详细解释 ENUM类型 ENUM 是一个字符串对象,其值通常选自一个允许值列表中,该列表在表创建时的列规格说明中被明确地列举. 在下列某些情况下,值也可以是空串(&quo ...
- mysql中enum的用法
字段 类型 长度/值*1 整理 属性 Null 默认2 额外 注释 enum 说明:enum类型的字段,若长度值写长度1/2,报错 (1) 数据长度为1,则为0,1,2… (2) ...
- spring中context:property-placeholder/元素 转载
spring中context:property-placeholder/元素 转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,passwo ...
- JAVA中enum的常见用法
JAVA中enum的常见用法包括:定义并添加方法.switch.遍历.EnumSet.EnumMap 1.定义enum并添加或覆盖方法 public Interface Behaviour{ void ...
- Java中enum的学习总结
一.通常的定义常量的方法 public class Sex{ public final static int MALE = 1; public final static int FEMALE=2; } ...
- SqlServer中的merge操作(转载)
SqlServer中的merge操作(转载) 今天在一个存储过程中看见了merge这个关键字,第一个想法是,这个是配置管理中的概念吗,把相邻两次的更改合并到一起.后来在technet上搜索发现别有 ...
- 关于python中Enum的个人总结
关于python中Enum的个人总结 初识 可以通过enum模块导入 语法 初始化: 可以通过enum_ = Enum('class_name', names,start = 1)来创建,其中name ...
- C++中enum(转载)
原文地址:http://www.cnblogs.com/ForFreeDom/archive/2012/03/22/2412055.html 1.为什么要用enum 写程序时,我们常常需要 ...
随机推荐
- SAP query传输以后须要又一次生成程序
近期有个需求,须要改动一个Query,在DEV改动好并測试通过后.传输到QAS,可是报表还是没变化,着实郁闷了一下,这是万能的google帮上忙了,原来传到其它系统以后还须要generate prog ...
- OpenERP 搜索过滤: 过去三个月
解决这个问题的重点在于 relativedelta 的应用 示例代码如下: 1: <!-- filter: last three months --> 2: <filter icon ...
- Flume、Kafka、Storm结合
Todo: 对Flume的sink进行重构,调用kafka的消费生产者(producer)发送消息; 在Sotrm的spout中继承IRichSpout接口,调用kafka的消息消费者(Consume ...
- Android编程之Fragment使用动画造成Unknown animation name: objectAnimator异常
在为Fragment做切换动画.启动后遇到了一个异常: Caused by: java.lang.RuntimeException: Unknown animation name: objectAni ...
- Angular 2 组件之间如何通信?
组件之间的共享可以有好几种方式 http://learnangular2.com/outputs/ 实例参考https://angular.io/docs/ts/latest/cookbook/com ...
- gulp安装使用
1. 安装nodeJs 2. 全局安装gulp: npm install gulp -g 3. 在cmd切换至项目文件夹下 npm init,创建package.json文件(JSON文件内不 ...
- imx6中iomux IO复用
IOMUX Controller (IOMUXC) IO多路复用控制器1.overviewThe IOMUX Controller (IOMUXC), together with the IOMUX, ...
- shiro session过期后ajax请求跳转(转)
配置了 Shrio框架,session也集成进去了 ,发现问题session会话过期,点击页面,一直请求失败.本来想集成拦截器,过滤器,但是已经用了shiro框架,sessionDestroyed 方 ...
- 【转】Monkey测试4——Monkey命令行可用的全部选项
Monkey命令行可用的全部选项 常规 --help 列出简单的用法. -v 命令行的每一个-v将增加反馈信息的级别. Level 0(缺省值)除启动提示.测试完成和最终结果之外,提供较少信息. Le ...
- 模式识别之概率分布---平均分布,正态分布,一阶滑动和,一阶线性回归 C语言编程
http://wenku.baidu.com/view/11cb1669a98271fe910ef9c6.html