ภาพรวม
เมธอด Enum.GetNames(Type) คืนค่า อาร์เรย์สตริง ที่มีชื่อของค่าคงที่ (constants) ทั้งหมดใน enum นั้น
รูปแบบการใช้งาน
enum Vehicle { Car, Motorbike, Truck }
string[] names = Enum.GetNames(typeof(Vehicle)); // ["Car", "Motorbike", "Truck"]
ตัวอย่างโค้ดเต็ม
using System;
class Demo
{
enum Vehicle { Car, Motorbike, Truck }
static void Main()
{
foreach (string res in Enum.GetNames(typeof(Vehicle)))
Console.WriteLine(res);
}
}
ผลลัพธ์
Car Motorbike Truck
เมธอด Enum ที่เกี่ยวข้อง
| เมธอด | คำอธิบาย | คืนค่า |
|---|---|---|
GetNames(Type) | ชื่อค่าคงที่ทั้งหมด | string[] |
GetValues(Type) | ค่าของค่าคงที่ทั้งหมด | Array (ต้อง cast) |
GetName(Type, value) | ชื่อของค่าเฉพาะ | string |
IsDefined(Type, value) | ตรวจสอบว่ามีค่านั้นใน enum | bool |
Parse(Type, string) | แปลงสตริงเป็นค่า enum | object (ต้อง cast) |
TryParse | แปลงสตริงเป็นค่า enum อย่างปลอดภัย | bool + out param |