1、遍历枚举
using System; namespace CSharp { public enum MissionType{ Runner1, Runner2, Runner3, Collertioner1, Collertioner2, Collertioner3, Player1, Player2, Player3 } public class MainClass { public static void Main1 (string[] args) { //遍历枚举 foreach (var item in Enum.GetValues(typeof( MissionType))) { Console.WriteLine (item); } } } }
运行结果:
2、获取枚举中某项中枚举中的Index
using System; namespace CSharp { public class EnemTest1 { public EnemTest1 () { } public static void GetIndex(MissionType missionType){ Console.WriteLine ( (int)missionType); } public static void Main (string[] args) { GetIndex(MissionType.Collertioner1); } } }
运行结果:
3、给定一个字符串,返回枚举类型
public static MissionType GetEnemType(string enemName){ //第三个参数指定是否大小写敏感 MissionType type=(MissionType)Enum.Parse(typeof(MissionType),enemName,true); Console.WriteLine ( type); return type; }
public static void Main (string[] args) { GetEnemType("Player1"); }
4、给定一个整形,返回字符串类型
//给定一个整形,返回字符串类型 public static string GetTypeStr(int index){ string str=((MissionType)index).ToString(); Console.WriteLine (str); return str; } public static void Main (string[] args) { GetTypeStr(1); }