当前位置: 移动技术网 > IT编程>开发语言>.net > C# enum枚举 枚举类 很全的枚举说明和使用

C# enum枚举 枚举类 很全的枚举说明和使用

2019年01月10日  | 移动技术网IT编程  | 我要评论

犬粮,安徽巢湖市,潼南租房网

enums枚举是值类型,数据直接存储在栈中,而不是使用引用和真实数据的隔离方式来存储。

(1)默认情况下,枚举中的第一个变量被赋值为0,其他的变量的值按定义的顺序来递增(0,12,3...),因此以下两个代码定义是等价的:

 enum trafficlight  

{  

 green,  

 yellow,  

 red  

}   

 enum trafficlight  

{  

 green = 0,  

 yellow = 1,  

red = 2  

}  


(2)enum枚举类型的变量的名字不能相同,但是值可以相同,例如:

 enum trafficlight  

{  

green = 0,  

yellow = 1,     // duplicate value, ok   

red = 1         // duplicate value, ok   

}  

(3)如果enum中的部分成员显式定义了值,而部分没有;那么没有定义值的成员还是会按照上一个成员的值来递增赋值,例如:

 enum looptype  

{  

  none,          // value is 0    

 daily,         // value is 1    

 weekly = 7,  

 monthly,       // value is 8    

yeayly,        // value is 9    

  daygap = 15,           

  weekgap,       // value is 16    

 monthgap,      // value is 17    

yeargap        // value is 18    

}  

(4)enum枚举成员可以用来作为位标志,同时支持位的操作(位与,位或等等),例如:

 

 enum carddecksettings : uint  

{  

 singledeck = 0x01,      // bit 0  

 largepictures = 0x02,   // bit 1  

fancynumbers = 0x04,    // bit 2  

animation = 0x08        // bit 3      

}  

十六进制数的一个作用就是用来进行位运算和操作,很方便。

 

 

1. 枚举(enum type)通常用来表示一组常量。由于枚举是强类型的,这在编程中给我们提供了极大的方便。

2. 枚举的定义:

 public enum sex
        {
            男 = 0,
            女 = 1
        }

或者:如果只给男赋值,那么女=1

 public enum sex
        {
            男 = 0,
            女 
        }

 

枚举在软件开发中的使用场景

在数据库设计人员表(person)时有性别字段sex(0代表男,1代表女),我们一般用bit或者int类型表示。

1.在编程时我们给sex字段赋值的方式为:

1).  sex=0;

2).  sex=(int)sexenum.man;

其中sexenum为定义性别的枚举类型,我们可以看出第二种方式的可读性更强。

2.在编程时我们,如果sex字段作为一个搜索条件的话,我们可能需要以下拉选择的方式展现所有可以选择的情况。那么我们就需要将sexenum转换成一个字典集合然后绑定到对应的select标签,具体怎么实现请看下面的示例代码。

………………………………

enum、int、string三种类型之间的互转

执行结果如下:

获取描述信息

 修改枚举如下:

获取描述信息代码如下:

打印结果如下:

 

枚举转换成字典集合的通用方法

1.这里我就直接列举代码如下:

复制代码
复制代码
 public static class enumhelper
    {
        /// <summary>
        /// 根据枚举的值获取枚举名称
        /// </summary>
        /// <typeparam name="t">枚举类型</typeparam>
        /// <param name="status">枚举的值</param>
        /// <returns></returns>
        public static string getenumname<t>(this int status)
        {
            return enum.getname(typeof(t), status);
        }
        /// <summary>
        /// 获取枚举名称集合
        /// </summary>
        /// <typeparam name="t"></typeparam>
        /// <returns></returns>
        public static string[] getnamesarr<t>()
        {
            return enum.getnames(typeof(t));
        }
        /// <summary>
        /// 将枚举转换成字典集合
        /// </summary>
        /// <typeparam name="t">枚举类型</typeparam>
        /// <returns></returns>
        public static dictionary<string, int> getenumdic<t>()
        {

            dictionary<string, int> resultlist = new dictionary<string, int>();
            type type = typeof(t);
            var strlist = getnamesarr<t>().tolist();
            foreach (string key in strlist)
            {
                string val = enum.format(type, enum.parse(type, key), "d");
                resultlist.add(key, int.parse(val));
            }
            return resultlist;
        }
        /// <summary>
        /// 将枚举转换成字典
        /// </summary>
        /// <typeparam name="tenum"></typeparam>
        /// <returns></returns>
        public static dictionary<string, int> getdic<tenum>()
        {
            dictionary<string, int> dic = new dictionary<string, int>();
            type t = typeof(tenum);
            var arr = enum.getvalues(t);
            foreach (var item in arr)
            {
                dic.add(item.tostring(), (int)item);
            }

            return dic;
        }

    }
复制代码
复制代码
复制代码
复制代码
  public enum sex
    {
        man,
        woman
    }
    public enum color
    {
        red,
        blue
    }
复制代码
复制代码

 

使用方法如下:

复制代码
复制代码
 static void main(string[] args)
        {
            var name = enumhelper.getenumname<sex>(1);
            console.writeline("数字转字符串:"+name);
            var dic1 = enumhelper.getenumdic<sex>();
            console.writeline("枚举转字典集合方法1:");
            foreach (var item in dic1)
            {
                console.writeline(item.key + "==" + item.value);
            }
            console.writeline("枚举转字典集合方法2:");
            var dic= enumhelper.getdic<color>();
            foreach (var item in dic)
            {
                console.writeline(item.key+"=="+item.value);
            }
            console.readline();
        }
复制代码
复制代码

 
转:https://www.cnblogs.com/eggtwo/p/5950131.html


 


  枚举类型声明为一组相关的符号常数定义了一个类型名称。枚举用于“多项选择”场合,就是程序运行时从编译时已经设定的固定数目的“选择”中做出决定。

  枚举类型(也称为枚举)为定义一组可以赋给变量的命名整数常量提供了一种有效的方法。例如,假设您必须定义一个变量,该变量的值表示一周中的一天。该变量只能存储七个有意义的值。若要定义这些值,可以使用枚举类型。枚举类型是使用 enum 关键字声明的。 

enum days { sunday, monday, tuesday, wednesday, thursday, friday, saturday };

默认情况下,枚举中每个元素的基础类型是 int。可以使用冒号指定另一种整数值类型。
如果不为枚举数列表中的元素指定值,则它们的值将以 1 为增量自动递增。在前面的示例中,days.sunday 的值为 0,days.monday 的值为 1,依此类推。创建新的 days 对象时,如果不显式为其赋值,则它将具有默认值 days.sunday (0)。创建枚举时,应选择最合理的默认值并赋给它一个零值。这便使得只要在创建枚举时未为其显式赋值,则所创建的全部枚举都将具有该默认值。枚举中大小写敏感,但是建议不要这样。

枚举的优点:
<1>枚举可以使代码更易于维护,有助于确保给变量指定合法的、期望的值。

<2>枚举使代码更清晰,允许用描述性的名称表示整数值,而不是用含义模糊的数来表示。

<3>枚举使代码更易于键入。在给枚举类型的实例赋值时,vs.net ide会通过intellisense弹出一个包含可接受值的列表框,减少了按键次数,并能够让我们回忆起可能的值

枚举实例

  声明: 



public enum timeofday
{
    moning = 0,
    afternoon = 1,
    evening = 2,
};


  使用: 



    public string gettimeofday(timeofday time)
    {
        string result = string.empty;
        switch (time)
        {
            case timeofday.moning:
                result = "上午";
                break;
            case timeofday.afternoon:
                result = "下午";
                break;
            case timeofday.evening:
                result = "晚上";
                break;
            default:
                result = "未知";
                break;
        }
        return result;
    }


枚举方法 



  <1>获取枚举字符串



timeofday time = timeofday.afternoon;

console.writeline(time.tostring());//输出:afternoon





  <2>enum.parse()方法。这个方法带3个参数,第一个参数是要使用的枚举类型。其语法是关键字typeof后跟放在括号中的枚举类名。typeof运算符将在第5章详细论述。第二个参数是要转换的字符串,第三个参数是一个bool,指定在进行转换时是否忽略大小写。最后,注意enum.parse()方法实际上返回一个对象引用—— 我们需要把这个字符串显式转换为需要的枚举类型(这是一个取消装箱操作的例子)。对于上面的代码,将返回1,作为一个对象,对应于timeofday.afternoon的枚举值。在显式转换为int时,会再次生成1。



timeofday time2 = (timeofday) enum.parse(typeof(timeofday), "afternoon", true);

console.writeline((int)time2);//输出1







  <3>得到枚举的某一值对应的名称

lbone.text = enum.getname(typeof(timeofday), 0);
    <4>得到枚举的所有的值 

foreach (int i in enum.getvalues(typeof(timeofday)))
            lbvalues.text += i.tostring();
    <5>枚举所有的名称 

foreach(string temp in enum.getnames(typeof(timeofday)))
            lbnames.text+=temp;
  

枚举和常量



  优先考虑枚举。

  在c#中,枚举的真正强大之处是它们在后台会实例化为派生于基类system.enum的结构。这表示可以对它们调用方法,执行有用的任务。注意因为.net framework的执行方式,在语法上把枚举当做结构是不会有性能损失的。实际上,一旦代码编译好,枚举就成为基本类型,与int和float类似。

  但是在实际应用中,你也许会发现,我们经常用英语定义枚举类型,因为开发工具本来就是英文开发的,美国人用起来,就直接能够明白枚举类型的含义。其实,我们在开发的时候就多了一步操作,需要对枚举类型进行。没办法,谁让编程语言是英语写的,如果是汉语写的,那我们也就不用翻译了,用起枚举变得很方便了。举个简单的例子,timeofday.morning一看到morning,美国人就知道是上午,但是对于中国的使用者来说,可能有很多人就看不懂,这就需要我们进行翻译、解释,就向上面的gettimeofday()的方法,其实就是做了翻译工作。所以,在使用枚举的时候,感觉到并不是很方便,有的时候我们还是比较乐意创建常量,然后在类中,声明一个集合来容纳常量和其意义。

  使用常量定义:这种方法固然可行,但是不能保证传入的参数day就是实际限定的。



using system;
using system.collections.generic;

public class timesofday
{
    public const int morning = 0;
    public const int afternoon = 1;
    public const int evening = 2;
    public static dictionary<int, string> list;
    /// <summary>
    /// 获得星期几
    /// </summary>
    /// <param name="day"></param>
    /// <returns></returns>
    public static string gettimenameofday(int time)
    {
        if (list == null || list.count <= 0)
        {
            list = new dictionary<int, string>();
            list.add(morning, "上午");
            list.add(afternoon, "下午");
            list.add(evening, "晚上");
        }

        return list[time];
    }
}










希望能够找到一种比较好的方法,将枚举转为我们想要的集合。搜寻了半天终于找到了一些线索。通过反射,得到针对某一枚举类型的描述。

枚举的定义中加入描述 

using system;
using system.componentmodel;

public enum timeofday
{
    [descrtion("上午")]
    moning,
    [description("下午")]
    afternoon,
    [description("晚上")]
    evening,
};


  获得值和表述的键值对

        /// <summary>
        /// 从枚举类型和它的特性读出并返回一个键值对
        /// </summary>
        /// <param name="enumtype">type,该参数的格式为typeof(需要读的枚举类型)</param>
        /// <returns>键值对</returns>
        public static namevaluecollection getnvcfromenumvalue(type enumtype)
        {
            namevaluecollection nvc = new namevaluecollection();
            type typedescription = typeof(descriptionattribute);
            system.reflection.fieldinfo[] fields = enumtype.getfields();
            string strtext = string.empty;
            string strvalue = string.empty;
            foreach (fieldinfo field in fields)
            {
                if (field.fieldtype.isenum)
                {
                    strvalue = ((int)enumtype.invokemember(field.name, bindingflags.getfield, null, null, null)).tostring();
                    object[] arr = field.getcustomattributes(typedescription, true);
                    if (arr.length > 0)
                    {
                        descriptionattribute aa = (descriptionattribute)arr[0];
                        strtext = aa.description;
                    }
                    else
                    {
                        strtext = field.name;
                    }
                    nvc.add(strtext, strvalue);
                }
            }
            return nvc;
        }




.net中flags枚举的使用

 .net中的枚举我们一般有两种用法,一是表示唯一的元素序列,例如一周里的各天;还有就是用来表示多种复合的状态。这个时候一般需要为枚举加上[flags]特性标记为位域,例如:




[flags] 
enum styles{ 
showborder = 1,         //是否显示边框
showcaption = 2,        //是否显示标题
showtoolbox = 4         //是否显示工具箱
} 




  这样我们就可以用"或"运算符组合多个状态,例如:





mycontrol.style = styles.showborder | styles.showcaption;  




  这时mycontrol.style枚举的值将变成 1+2=3,它的tostring()将变成"styles.showborder , styles.showcaption" 
这里我们可以解释为什么第三个值showtoolbox可以为4,5..而不能为3。也就是说它的值不应该是前几项值的复合值。有一个比较简单的方法就是用2的n次方来依次为每一项赋值,例如 1,2,4,8,16,32,64..... 

现在举个常见的flags应用例子。例如一个简单的权限系统,有"admin"和"user"两种角色,我们可以在表中放一个 varchar()字段,以文本形式存放权限字"admin,user"。但是用flags型枚举的话,我们就可以直接将 roles.admin | roles.user 的值放在一个int字段里。 

以下是关于枚举的一些常见操作: 
将枚举的值变回枚举对象: 
styles style = (styles) enum.parse(typeof(styles), 4 );    // -> style = styles.toolbox; 
  检查枚举是否包含某个元素: 
bool hasflag = ((style & styles.showborder) != 0); 

其实我们还会碰到一种情况,就是需要从组合状态中去掉一个元素。用"^"运算符可以做到:





styles style = styles.showborder | styles.showcaption; 
style = style ^ styles.showborder; 





  这个时候style的值就会变成 styles.showcaption 

但这里有一个很严重的问题(偶现在才发现) 
我们这个时候再执行一次 
style = style ^ styles.showborder; 
按照我们的设想,这个时候 style 的值是 styles.showcaption,不包含 styles.showborder,所以我们就算去掉这个元素,style应该还是不会变。但实际的 style 的值却又变成了 styles.showborder | styles.showcaption !! 再执行一遍,又会去掉这个元素,周而复始。 
当然我们可以在去掉某个元素前做一番检查,如果枚举包含这个元素,再去掉它:





if ((style & styles.showborder) != 0){ 
style = style ^ styles.showborder; 
} 





  不知道有没有其它方法可以方便地从flags枚举状态中去掉一个元素。。 

  thanks to mobilebilly: 
style = style & (~styles.showborder) 可以方便去掉一个元素。



好好利用枚举

      这段时间手里有个有关订单的项目,订单一般有个状态的,以前很多要时候都会想到订单的状态就那几个种,就把它写死吧,不用一个表了,太浪费资源了,但写死了用一个数字来代表一种订单状态,这样在编码时还要记得什么数字代码什么状态,如果不小心把它写错了,会导致数据出错。
      后来想到.net有个枚举,这么好的东西为何不用上来呢,这不但可以方便以后的代码维护,也方便编码。

public enum orderstate
{
    /// <summary>
    /// 无效状态
    /// </summary>
    invalid = 0,
    /// <summary>
    /// 客户询价
    /// </summary>
    customerquery = 1,
    /// <summary>
    /// 客户落单
    /// </summary>
    customerordered = 2,
    /// <summary>
    /// 客户付款
    /// </summary>
    receivercustomerpayment = 4,
    /// <summary>
    /// 向供货商订货
    /// </summary>
    supplierordered = 8,
    /// <summary>
    /// 供货商确认货期
    /// </summary>
    supplierorderterm = 16,
    /// <summary>
    /// 收到货品
    /// </summary>
    recievergoods = 32,
    /// <summary>
    /// 客户取消订单
    /// </summary>
    customercanceled = 64,
    /// <summary>
    /// 供货商取消订单
    /// </summary>
    suppliercancelded = 128
}
但要从ui层看这些状态怎么处理呢?
利用switch case

public static string getorderstatestring(orderstate state)
    {
        switch (state)
        {
            case orderstate.invalid:
                return "无效值";
            case orderstate.customerordered:
                return "客户下单";
            case orderstate.customercanceled:
                return "客户取消订单";
            case orderstate.customerquery:
                return "客户询价";
            case orderstate.receivercustomerpayment:
                return "客户已付款";
            case orderstate.recievergoods:
                return "订单到货";
            case orderstate.suppliercancelded:
                return "供货商取消";
            case orderstate.supplierordered:
                return "已向供货商订货";
            case orderstate.supplierorderterm:
                return "供货商确认货期";
        }
        return "";
    }


如果以后还有更多的订单状态就修改这个枚举和一个方法就行了,这么方便的东西为何就不用到我的程序中呢,我们在编码中,要想尽方法使代码简单、易用、易维护。
枚举中有两个很实用的方法
1、gethashcode()   //返回该实例的值的哈希代码
2、tostring()           //将此实例的值转换为其等效的字符串表示

这两个方法在编码的时候会用到,gethashcode()这个方法使用机会会更多。

本文来自csdn博客,转载请标明出处:http://blog.csdn.net/gulijiang2008/archive/2009/12/23/5061442.x

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网