C# 列舉 GetValues 方法


獲取指定列舉中常量的值的陣列。

這是我們的列舉。

enum Rank { Jack = 10, Tom = 19, Tim = 26 };

現在,使用 GetValues() 方法獲取列舉的所有值並顯示為陣列。

foreach(int res in Enum.GetValues(typeof(Rank))) {
   Console.WriteLine(res);
}

讓我們看整個示例。

示例

 即時演示

using System;
public class Demo {
   enum Rank { Jack = 10, Tom = 19, Tim = 26 };
   public static void Main() {
      Console.WriteLine("Here are the university rank of MCA Students College ABC:");
      foreach(int res in Enum.GetValues(typeof(Rank))) {
         Console.WriteLine(res);
      }
   }
}

輸出

Here are the university rank of MCA Students College ABC:
10
19
26

更新於: 2020-06-23

2 千次瀏覽

開啟你的職業生涯

透過完成課程以獲取證書

入門
廣告
© . All rights reserved.