C# enum、int、string三种类型互相转换

时间:2023-03-08 16:30:26
C# enum、int、string三种类型互相转换

enum、int、string三种类型之间的互转

#代码:

 public enum Sex
{
Man=,
Woman=
} public static void enumConvert()
{
int sexNum = (int)Sex.Man;
Console.WriteLine("将枚举转换成整数:"+sexNum);//1 string sexStr = Sex.Woman.ToString();
Console.WriteLine("将枚举转换成字符串:"+sexStr);//"Woman" Sex sexStrEnum = (Sex)Enum.Parse(typeof(Sex),"Man");
Console.WriteLine("将字符串转换成枚举:" + sexStrEnum.ToString());//"Man" Sex sexNumEnum = (Sex);
Console.WriteLine("将整数转换成枚举:"+sexNumEnum.ToString());//"Man" string numToStr = Enum.GetName(typeof(Sex),);
Console.WriteLine("将整数转换成字符串:"+numToStr);//"Woman" int strToNum = (int)Enum.Parse(typeof(Sex), "Woman");
Console.WriteLine("将字符串转换成整数:" + strToNum);//2
}

#结果:

C# enum、int、string三种类型互相转换

      willingtolove

***————————————————***