zoukankan      html  css  js  c++  java
  • c#枚举的用法及遍历方法

    来自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 
  • 相关阅读:
    bash 中有效建立锁
    go 语言 Makefile 指定依赖包位置
    在 mysql 中对特定的库禁用 DDL 语句
    go 语言并发机制 goroutine 初探
    Google和facebook如何应用R进行数据挖掘
    数据应用催生商业模式
    4款语音播报来电短信应用[Android]
    让 php 用 nginx 打包 zip
    10个关于 Dropbox 的另类功用(知乎问答精编)[还是转来了]
    分析以数据挖掘技术预测用户流失情况的方法
  • 原文地址:https://www.cnblogs.com/figo/p/1122319.html
Copyright © 2011-2022 走看看