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 写程序时,我们常常需要 ...
随机推荐
- 使用zbar 解码二维码 条形码
#!/usr/bin/env python # coding: u8 import os import zbar import Image import urllib import uuid def ...
- 使用Nginx+uWSGI+Django方法部署Django程序(上)
Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器最前端,它将接收WEB的所有请求,统一管理请求.ng ...
- linux 修改时间
实例:设置时间伟2008年8月8号12:00# date -s "2008-08-08 12:00:00"修改完后,记得执行clock -w,把系统时间写入CMOS date -s ...
- TCP/IP ---分层
TCP/IP的分层 ICMP是IP协议的附属协议.IP层用它来与其他主机或路由器交换错误报文和其他重要信息.尽管ICMP主要被IP使用,但应用程序也有可能访问它.我们将分析两个流行的诊断工具,Ping ...
- sql server 常用函数 及 方法
返回受上一语句影响的行数: @@ROWCOUNT 语法@@ROWCOUNT 返回类型integer 注释任何不返回行的语句将这一变量设置为 0 ,如 IF 语句. 示例下面的示例执行 UPDATE 语 ...
- Linux入门常用指令
linux获取硬盘和目录大小的命令 df命令 Linux下可以用df命令获取硬盘的使用情况,通过man可以获取df命令的详细情况.df命令经常用的参数为: a:显示全部的档案系统和各分割区的磁盘使用情 ...
- fpga技能树
- C语言基础(21)-C语言编译过程及GCC参数简介
任何C语言的编译过程可分为以下三部分: 一.预编译 在C语言中,以#开头的语句又叫预编译指令.预编译主要做以下两件事情: 1.将#include包含的头文件做简单的文本替换: 2.将代码中的注释删除. ...
- C# 运行时中的泛型
将泛型类型或方法编译为 Microsoft 中间语言 (MSIL) 时,它包含将其标识为具有类型参数的元数据. 泛型类型的 MSIL 的使用因所提供的类型参数是值类型还是引用类型而不同. 第一次用值类 ...
- CentOS 6.2 安装kdbg
地址:http://pkgs.org/centos-5/centos-x86_64/kdbg-2.0.2-1.2.1.x86_64.rpm.html 百度网盘地址:http://pan.baidu.c ...