zoukankan      html  css  js  c++  java
  • 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, Thursday, 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 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"));
     
             Console.WriteLine();
             Console.WriteLine("Enums can also be created which have values that represent some meaningful amount.");
             Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:");
     
             foreach ( string s in Enum.GetNames(boiling) )
                 Console.WriteLine( "{0,-11}= {1}", s, Enum.Format(boiling, Enum.Parse(boiling, s), "d"));
     
             Colors myColors = Colors.Red | Colors.Blue | Colors.Yellow;
             Console.WriteLine();
             Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors);
         }
     }
    url:http://greatverve.cnblogs.com/archive/2012/06/06/Enum.html
  • 相关阅读:
    Servlet的生命周期?
    C++图结构的图结构操作示例
    如何从google play下载app应用,直接下载apk
    C# Socket异步聊天例子
    三极管饱和,放大,截止电压判断
    java中的浮点(float)运算
    微软2014校园招聘笔试试题
    软件开发中的资源控制问题学习
    linux mount命令学习
    17、Spring Boot普通类调用bean【从零开始学Spring Boot】
  • 原文地址:https://www.cnblogs.com/greatverve/p/Enum.html
Copyright © 2011-2022 走看看