Enum 各种使用方法

来源:互联网 发布:三甲医院体检知乎 编辑:程序博客网 时间:2024/06/05 16:56
  

enum EnumT { Acc, BCC, Dcc };
        private void button2_Click(object sender, EventArgs e)
        {
            foreach (string item in Enum.GetNames(typeof(EnumT)))
            {
                this.comboBox1.Items.Add(item);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch ((EnumT)Enum.Parse(typeof(EnumT), this.comboBox1.Text))
            {
                case EnumT.Acc:
                    break;
                case EnumT.BCC:
                    break;
                case EnumT.Dcc:
                    break;
                default:
                    break;
            }
        }

        enum enu { a = 1, b = 2, c = 3, d = 4 };
        private void button3_Click(object sender, System.EventArgs e)
        {
            //foreach  
            foreach (string s in Enum.GetNames(typeof(enu)))
            {
                Console.WriteLine(s);//a b c d
            }

            foreach (int s in Enum.GetValues(typeof(enu)))
            {
                Console.WriteLine(s);//1 2 3 4
            }

            //to   Array[]  
            string[] en = Enum.GetNames(typeof(enu));//a b c d
            Array ev = Enum.GetValues(typeof(enu));//a b c d
        }

----------网络上的-----------

 

using  System;  
 
public  class  EnumTest  {  
       
enum  Days  {  Saturday,  Sunday,  Monday,  Tuesday,  Wednesday,  Thursday,  Friday  };  
 
       
public  static  void  Main()  {  
 
               Type  weekdays  
=  typeof(Days);  
 
               Console.WriteLine(
"The  days  of  the  week,  and  their  corresponding  values  in  the  Days  Enum  are:");  
 
               
foreach  (  string  s  in  Enum.GetNames(weekdays)  )  
                       Console.WriteLine(  
"{0,-11}=  {1}",  s,  Enum.Format(  weekdays,  Enum.Parse(weekdays,  s),  "d"));  
       }  
}  

 

来自MSDN
枚举可用来存储字符串与数字的值对,相当于一个对照表
常用方法:GetName(),GetValue(),Parse()
 1 using System;
 2 
 3 public class EnumTest {
 4     enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
 5     enum BoilingPoints { Celcius = 100, Fahrenheit = 212 };
 6     [FlagsAttribute]
 7     enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };
 8 
 9     public static void Main() {
10 
11         Type weekdays = typeof(Days);
12         Type boiling = typeof(BoilingPoints);
13 
14         Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:");
15 
16         foreach ( string s in Enum.GetNames(weekdays) )
17             Console.WriteLine( "{0,-11}= {1}", s, Enum.Format( weekdays, Enum.Parse(weekdays, s), "d"));
18 
19         Console.WriteLine();
20         Console.WriteLine("Enums can also be created which have values that represent some meaningful amount.");
21         Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:");
22 
23         foreach ( string s in Enum.GetNames(boiling) )
24             Console.WriteLine( "{0,-11}= {1}", s, Enum.Format(boiling, Enum.Parse(boiling, s), "d"));
25 
26         Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow;
27         Console.WriteLine();
28         Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors);
29     }
30 }
31 
32 
 

如何把string类型转为enum类型

定义了enum类型如下:
    public enum InteractiveType
    
{
        Nothing 
= 1,//什么也不干
        Email = 2,//通过email传回
        Jiwai = 3//通过sms传回(通过api更新叽歪,然后叽歪的关注功能传回)
    }
 

如果我们想把字符串"Email"或者数字转化为该enum类型,如何转换呢?
可以如下转换:
(InteractiveType)Enum.Parse(typeof(InteractiveType), "Email"false)

最后一个参数是指是否忽略大小写匹配,false指考虑大小写;同样的,这里字符串"Email"也可以换成数值"2"。