C# convert enum value to String

  • 143
  • 0

筆記下列舉值轉成字串形式的對應數字

結論

.ToString("D")

範例

Enum

public enum Urgency 
{ 
    VeryHigh = 1,
    High = 2,
    Low = 4
}

WriteLine

Console.WriteLine(Urgency.High.ToString("D"));

Output

"2"

參照

C# numeric enum value as string - Stack Overflow

PS5