C# में Type.GetEnumNames() विधि का उपयोग वर्तमान गणना प्रकार के सदस्यों के नाम वापस करने के लिए किया जाता है।
सिंटैक्स
निम्नलिखित वाक्य रचना है -
public virtual string[] GetEnumNames ();
उदाहरण
आइए अब Type.GetEnumNames() विधि को लागू करने के लिए एक उदाहरण देखें -
using System;
public class Demo {
enum Vehicle {Car, Bus, Bike}
public static void Main(){
try {
Type type = typeof(string);
string[] str = type.GetEnumNames();
Console.WriteLine("GetEnumName() to return the constant name = " + str);
Console.WriteLine("Listing constants ..");
for (int i = 0; i < str.Length; i++)
Console.Write("{0} ", str[i]);
}
catch (ArgumentException e){
Console.WriteLine("Not an enum!");
Console.Write("{0}", e.GetType(), e.Message);
}
}
} आउटपुट
यह निम्नलिखित आउटपुट देगा -
Not an enum! System.ArgumentException
उदाहरण
आइए अब Type.GetEnumNames() विधि को लागू करने के लिए एक और उदाहरण देखें -
using System;
public class Demo {
enum Vehicle {Car, Bus, Bike, airplane}
public static void Main(){
try {
Vehicle v = Vehicle.Bike;
Type type = v.GetType();
string[] str = type.GetEnumNames();
Console.WriteLine("GetEnumName() to return the constant name = " + str);
Console.WriteLine("Listing constants ..");
for (int i = 0; i < str.Length; i++)
Console.Write("{0} ", str[i]);
}
catch (ArgumentException e){
Console.WriteLine("Not an enum!");
Console.Write("{0}", e.GetType(), e.Message);
}
}
} आउटपुट
यह निम्नलिखित आउटपुट देगा -
GetEnumName() to return the constant name = System.String[] Listing constants .. Car Bus Bike airplane