c#枚举(Enum)的用法及遍历方法

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

c#枚举(Enum)的用法及遍历方法

foreach (string s in Enum.GetNames(typeof(WallKind)))

{

WinFormTools.MsgBox(s);

}

有人问怎样遍历Revit API中的枚举,遍历枚举是C#的语法功能。

来自MSDN

枚举可用来存储字符串与数字的值对,相当于一个对照表

常用方法:GetName(),GetValue(),Parse()

using System;

public class EnumTest {

enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursda y, Friday };

enum BoilingPoints { Celcius = 100, Fahrenheit = 212 };

[FlagsAttribute]

enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };

public static void Main() {

Type weekdays = typeof(Days);

Type boiling = typeof(BoilingPoints);

Console.WriteLine("The days of the week, and their corresponding valu es in the Days Enum are:");

foreach ( string s in Enum.GetNames(weekdays) )

Console.WriteLine( "{0,-11}= {1}", s, Enum.Format( weekdays, Enu m.Parse(weekdays, s), "d"));

Console.WriteLine();

Console.WriteLine("Enums can also be created which have values that r epresent some meaningful amount.");

Console.WriteLine("The BoilingPoints Enum defines the following item s, and corresponding values:");

foreach ( string s in Enum.GetNames(boiling) )

Console.WriteLine( "{0,-11}= {1}", s, Enum.Format(boiling, Enum.P arse(boiling, s), "d"));

Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow;

Console.WriteLine();

Console.WriteLine("myColors holds a combination of colors. Namely: {0} ", myColors);

}

}

相关文档
最新文档