惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
月光博客
月光博客
S
SegmentFault 最新的问题
量子位
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
博客园 - Franky
Google DeepMind News
Google DeepMind News
Recent Announcements
Recent Announcements
B
Blog RSS Feed
C
Check Point Blog
The Cloudflare Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
F
Fortinet All Blogs
Hugging Face - Blog
Hugging Face - Blog
博客园 - 叶小钗
V
Visual Studio Blog
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 聂微东
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 费哥

VC++实现应用程序对插件的支持(转) typedef的四个用途和两个陷阱(转) c#动态创建ODBC数据源 设为首页,加入收藏,联系我们 - 费哥 - 博客园 ASP.NET 2.0中CSS失效 ADO.NET更新ACCESS碰到的怪异问题 DataGrid中的數據輸出到Excel 对 Windows 窗体控件进行线程安全调用 将参数传递给线程(Vc#2005) 创建合理位置的线标注 Sql2005导出SQL2000格式的脚本 将外部图块插入当前图形(c#代码) 无法将顶级控件添加到控件 使用Microsoft Visual Studio2005开发ObjectARX设置 CS1595:已在多处定义 软件设计师考试大纲 <<设计模式可复用面向对象软件的基础>>--组合模式(Composite) <<设计模式可复用面向对象软件的基础>>--设计模式怎样解决设计问题 <<设计模式可复用面向对象软件的基础>>读书笔记--(第一章)引言
c#枚举的用法及遍历方法
费哥 · 2008-03-26 · via 博客园 - 费哥

来自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