C# Enum GetName 方法


GetName() 方法會返回列舉中的常量名稱。

這是一個列舉。

enum Stock { Appliance, Clothing, Footwear };

現在,使用 Enum.GetName() 方法獲取名稱。只需設定常量,即可檢索單個名稱。

Enum.GetName(typeof(Stock), 1

現在,讓我們來看一個示例。

例如

 線上示例

using System;
class Demo {
   enum Stock { Appliance, Clothing, Footwear };
   static void Main() {
      Console.WriteLine("The value of second stock category = {0}",Enum.GetName(typeof(Stock), 1));
      Console.WriteLine("The value of third stock category = {0}",Enum.GetName(typeof(Stock), 2));
   }
}

輸出

The value of second stock category = Clothing
The value of third stock category = Footwear

更新於:2020-06-23

1K+ 瀏覽量

開啟您的 職業生涯

完成該課程即可獲得認證

開始學習
廣告
© . All rights reserved.