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. 配置git账号和密码

    最开始启动的时候 配置用户名和用户邮箱,安装完git第一件要做的事情! git config --global uesr.name "Sunnshino" git config - ...

  2. NodeJS实战——创建基础应用并应用模板引擎

    本次的目的是搭建一个最基础忽地可以实现功能的NodeJSserver,可以体现出NodeJS的工作流程以及开发的基本框架. 需求:已经安装了nodejs以及express. 一.构建基础的NodeJS ...

  3. node.js 学习02

    读写文件中的路径问题 readFile()读取文件函数中的./(相对路径)这个参数,相对的是执行node命令的路径,而不是相对于正在执行的这个js文件来查找.为了解决这个问题: __dirname(两 ...

  4. Apache Rewrite 规则详解

    在开篇之前: 我想说这篇文章其实是我刚刚接触Rewrite的时候学习的文档,应属转载,但是在这里我不想写明原地址,原因是文章中大多数给出的配置命令经实验都是错误的.需要原文的可以在谷歌上搜索一下&qu ...

  5. eclipse日志

    注意包别引入错: import org.slf4j.Logger; import org.slf4j.LoggerFactory; private static final Logger log = ...

  6. TensorFlow学习笔记4——变量共享

    因为最近在研究生成对抗网络GAN,在读别人的代码时发现了 with tf.variable_scope(self.name_scope_conv, reuse = reuse): 这样一条语句,查阅官 ...

  7. mac下使用QuickTime录屏及上传youku注意事项

    一,解决QuickTime录屏不能带声音的问题: mac下使用QuickTime屏幕 Soundflower->Audio Setup->soundflower(2ch),在其上鼠标右键, ...

  8. sqlite or svn 错误 The database disk image is malformed 可解决

    在网上找了很多资料,很多网友都提到这个问题是不可解决的,面对这个问题,只能作罢. 但我不甘心这么丢失数据,最最后找到了一个解决方法.经测试,原来数据,全部保住. 以下为原文. http://www.s ...

  9. linux系统下crontab 配置启动定时任务

    1 crontab -e 配置启动定时任务 */1 * * * * sh /home/admin/application/wd/core-python/getMemPositionFromAnaual ...

  10. 【spring boot】在spring boot下使用多线程

    使用场景: 方法处理到某一步,需要将信息交给另一个线程去处理!! =================================================================== ...