1、关于enum的定义
enum Fabric
{
Cotton = 1,
Silk = 2,
Wool = 4,
Rayon = 8,
Other = 128
}
2、符号名和常数值的互相转换
 
            Fabric fab = Fabric.Cotton;
            int fabNum = (int)fab;//转换为常数值。必须使用强制转换。
            Fabric fabString = (Fabric)1;//常数值转换成符号名。如果使用ToString(),则是((Fabric)1).ToString(),注意必须有括号。
            string fabType = fab.ToString();//显示符号名
            string fabVal = fab.ToString ("D");//显示常数值
 
3、获得所有符号名的方法(具体参见Enum类)
 
        public enum MyFamily
        {
            YANGZHIPING = 1,
            GUANGUIQIN = 2,
            YANGHAORAN = 4,
            LIWEI = 8,
            GUANGUIZHI = 16,
            LISIWEN = 32, 
            LISIHUA = 64,
        }
 
            foreach (string s in Enum.GetNames(typeof(MyFamily)))
            {
                Console.WriteLine(s);
            }
 
4、将枚举作为位标志来处理
根据下面的两个例子,粗略地说,一方面,设置标志[Flags]或者[FlagsAttribute],则表明要将符号名列举出来;另一方面,可以通过强制转换,将数字转换为符号名。说不准确。看下面的例子体会吧。注意:
          例一:
          Fabric fab = Fabric.Cotton | Fabric.Rayon | Fabric.Silk;
          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 - 8

All 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
*/

5、枚举作为函数参数。经常和switch结合起来使用。下面举例

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的总结(转载)的更多相关文章

  1. MVC图片上传详解 IIS (安装SSL证书后) 实现 HTTP 自动跳转到 HTTPS C#中Enum用法小结 表达式目录树 “村长”教你测试用例 引用provinces.js的三级联动

    MVC图片上传详解   MVC图片上传--控制器方法 新建一个控制器命名为File,定义一个Img方法 [HttpPost]public ActionResult Img(HttpPostedFile ...

  2. MYSQL中 ENUM 类型

    MYSQL中 ENUM 类型的详细解释 ENUM类型 ENUM 是一个字符串对象,其值通常选自一个允许值列表中,该列表在表创建时的列规格说明中被明确地列举. 在下列某些情况下,值也可以是空串(&quo ...

  3. mysql中enum的用法

    字段 类型 长度/值*1 整理 属性 Null 默认2 额外 注释 enum         说明:enum类型的字段,若长度值写长度1/2,报错 (1)  数据长度为1,则为0,1,2… (2)   ...

  4. spring中context:property-placeholder/元素 转载

    spring中context:property-placeholder/元素  转载 1.有些参数在某些阶段中是常量 比如 :a.在开发阶段我们连接数据库时的连接url,username,passwo ...

  5. JAVA中enum的常见用法

    JAVA中enum的常见用法包括:定义并添加方法.switch.遍历.EnumSet.EnumMap 1.定义enum并添加或覆盖方法 public Interface Behaviour{ void ...

  6. Java中enum的学习总结

    一.通常的定义常量的方法 public class Sex{ public final static int MALE = 1; public final static int FEMALE=2; } ...

  7. SqlServer中的merge操作(转载)

    SqlServer中的merge操作(转载)   今天在一个存储过程中看见了merge这个关键字,第一个想法是,这个是配置管理中的概念吗,把相邻两次的更改合并到一起.后来在technet上搜索发现别有 ...

  8. 关于python中Enum的个人总结

    关于python中Enum的个人总结 初识 可以通过enum模块导入 语法 初始化: 可以通过enum_ = Enum('class_name', names,start = 1)来创建,其中name ...

  9. C++中enum(转载)

    原文地址:http://www.cnblogs.com/ForFreeDom/archive/2012/03/22/2412055.html 1.为什么要用enum       写程序时,我们常常需要 ...

随机推荐

  1. STL坑汇总

    1. Q:vector的push_back()方法到底做了些什么? 为什么声明写的是void push_back (const value_type& val); A:的确,乍一看,似乎pus ...

  2. jetty.xml解析

    我们知道jetty有一种启动方式是在jetty的根目录中运行命令行:java -jar start.jar,这个命令会调用apache的XmlConfiguration工具类作为启动类,这个类会默认读 ...

  3. SVN环境搭建(1)

    原文地址:http://www.penglig.com/post-72.html Subversion 是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建 SVN 服务 ...

  4. HTML5特性

    HTML5规范围绕如何使用新增标记定义了大量Javascript API,其中一些API与DOM重叠,定义了浏览器应该支持DOM拓展. 1.与类相关的扩充 HTML5新增了getElementsByC ...

  5. 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程

    反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑)   背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...

  6. cassandra的primary key, partition key, cluster key,

    https://stackoverflow.com/questions/24949676/difference-between-partition-key-composite-key-and-clus ...

  7. arm linux c++11编译

    include(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11 ...

  8. sql语句单据编号生成防并发

    有用户反馈说发现重复单据号,检查发现以下单据号被分配给了不同的两个职工 系统中使用语句exec GetNewOrderNumber 'pwgnumber','PWG',1, @pwg_number o ...

  9. Android 升级ADT到22第三方Jar包导致的ClassNotFoundException和NoClassDefFoundError异常解决

    在使用异步载入框架Android-Universal-Image-Loader的Jar包的时候遇到错误: java.lang.NoClassDefFoundError:com.nostra13.uni ...

  10. Java客户端Jedis

    使用Jedis的Java客户端 maven依赖 <!-- jedis --> <dependency> <groupid>redis.clients</gro ...