C# 枚举小结

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    public enum TimeOfDay 
    { 
        Moning = 0, 
        Afternoon = 1, 
        Evening = 2, 
    }; 
    class Program
    {
        static void Main(string[] args)
        {
            TimeOfDay time = TimeOfDay.Afternoon;
            //举常用方法<1>获取枚举字符串
            Console.WriteLine(time.ToString());//输出:Afternoon 
            TimeOfDay time2 = (TimeOfDay)

            //枚举常用方法<2>Enum.Parse()方法
            Enum.Parse(typeof(TimeOfDay), "afternoon", true);
            Console.WriteLine((int)time2);//输出1 

            //枚举常用方法<3>得到枚举的某一值对应的名称
            string mm = Enum.GetName(typeof(TimeOfDay), 0);
            
            Console.WriteLine("mm等于多少{0}",mm);

            
           // string nn = Enum.GetName(typeof(TimeOfDay), 0);
           // Console.WriteLine("nn等于多少{0}", nn);


            //C# 枚举常用方法<4>得到枚举的所有的值
            string kg="", kgkg="";
            foreach (int i in Enum.GetValues(typeof(TimeOfDay)))
            { kg += i.ToString(); kg += ' '; }
            Console.WriteLine("kg等于多少{0}", kg);

            //C# 枚举常用方法<5>枚举所有的名称
            foreach (string temp in Enum.GetNames(typeof(TimeOfDay)))
            { kgkg += temp; kgkg += ' '; }
             
            Console.WriteLine("kgkg等于多少{0}", kgkg);
            
        }
    }
}


 

你可能感兴趣的:(C# 枚举小结)