รับอาร์เรย์ของค่าคงที่ในการแจงนับที่ระบุ
นี่คือ enum ของเรา
enum Rank { Jack = 10, Tom = 19, Tim = 26 }; ตอนนี้ รับค่าทั้งหมดของ enum เป็นอาร์เรย์และแสดงโดยใช้เมธอด 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