C# 枚举 Parse 方法
Enum 中的 Parse 方法将枚举常量名称或数值的字符串表示形式转换为等效的枚举对象。
以下为我们的枚举。
enum Vehicle { Car, Bus, Truck, Motobike };现在,在循环中使用 GetNames() 方法获取枚举值。使用 Enum.Parse() 方法分析它们,如下所示 −
Enum.Parse(typeof(Vehicle)
示例
using System;
public class Demo {
enum Vehicle { Car, Bus, Truck, Motobike };
public static void Main() {
Console.WriteLine("The enumeration...");
foreach (string v in Enum.GetNames(typeof(Vehicle))) {
Console.WriteLine("{0} = {1:D}", v, Enum.Parse(typeof(Vehicle), v));
}
Console.WriteLine();
}
}输出
The enumeration... Car = 0 Bus = 1 Truck = 2 Motobike = 3
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP