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
}
----------网络上的-----------
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"));
}
}
枚举可用来存储字符串与数字的值对,相当于一个对照表
常用方法:GetName(),GetValue(),Parse()
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类型
{
Nothing = 1,//什么也不干
Email = 2,//通过email传回
Jiwai = 3//通过sms传回(通过api更新叽歪,然后叽歪的关注功能传回)
}
如果我们想把字符串"Email"或者数字转化为该enum类型,如何转换呢?
可以如下转换:
最后一个参数是指是否忽略大小写匹配,false指考虑大小写;同样的,这里字符串"Email"也可以换成数值"2"。
- Enum 各种使用方法
- Enum枚举的原理及各种使用方法
- Enum使用方法
- enum的使用方法
- java Enum的使用方法
- enum 的使用方法
- JAVA中enum使用方法
- enum 枚举的使用方法
- enum类使用方法
- Enum使用方法,为什么使用typedef enum--转载
- JPA enum(枚举)的使用方法
- 枚举型enum的使用方法
- Java enum(枚举)使用方法详解
- enum枚举的类型和使用方法
- Android 各种控件使用方法
- DSPack各种使用方法
- DSPack各种使用方法
- Toast的各种使用方法
- 失败应聘的五大原因
- jquery 禁用右键菜单 检测browser 检查存在的元素
- MFC--自定义分割条颜色以及求分割条大小方法
- linux 安装 oracle 常见问题
- 利用cx_Freeze将py文件打包成exe文件
- Enum 各种使用方法
- 一个奇怪的CRASH问题
- 如何看懂源代码--(分析源代码方法)
- python解析xml文档实例
- QQ通信协议
- Redis命令参考简体中文版
- 如何记录并分析CRASH日志方法一
- CString、string、char*、int 类型之间转换总结
- Keil MDK的安装与工程建立