Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> C#

C# में वर्तमान एन्यूमरेशन प्रकार का अंतर्निहित प्रकार प्राप्त करें

Enum.GetUnderlyingType() या Type.GetEnumUnderlyingType() मेथड एन्यूमरेशन के अंतर्निहित पूर्णांक प्रकार (डिफ़ॉल्ट रूप से int, लेकिन byte, short, long, आदि हो सकता है) लौटाता है।

उदाहरण 1: वैध एनम

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("GetEnumNames() to return the constant names = " + string.Join(", ", str));
            Type type2 = type.GetEnumUnderlyingType();
            Console.Write("Enum Underlying type = " + type2);
            Console.WriteLine("\nListing 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);
        }
    }
}

आउटपुट

GetEnumNames() to return the constant names = Car, Bus, Bike, Airplane
Enum Underlying type = System.Int32
Listing constants ..
Car Bus Bike Airplane

उदाहरण 2: गैर-एनम प्रकार (int)

using System;

public class Demo
{
    enum Vehicle { Car, Bus, Bike, Airplane }
    public static void Main()
    {
        try
        {
            Type type = typeof(int);
            string[] str = type.GetEnumNames();
            Console.WriteLine("GetEnumNames() to return the constant names = " + string.Join(", ", str));
            Type type2 = type.GetEnumUnderlyingType();
            Console.Write("Enum Underlying type = " + type2);
            Console.WriteLine("\nListing 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

GetEnumUnderlyingType केवल एनम प्रकारों पर काम करता है; अन्यथा ArgumentException फेंकता है। अंतर्निहित प्रकार एनम घोषणा में निर्दिष्ट होता है: enum MyEnum : long { ... }। डिफ़ॉल्ट int है। Enum.GetValues और Enum.GetNames संबंधित मेथड्स हैं।

  1. - C# में वर्तमान एन्यूमरेशन प्रकार के सदस्यों के नाम प्राप्त करें

    Type.GetEnumNames() मेथड एन्यूमरेशन स्थिरांक के नामों का एक सरणी लौटाता है। यह केवल एनम प्रकारों पर काम करता है; अन्यथा ArgumentException फेंकता है। उदाहरण 1: गैर-एनम प्रकार (string) using System; public class Demo { enum Vehicle { Car, Bus, Bike } public static void Main() { tr

  1. - C# में वर्तमान इंस्टेंस का प्रकार प्राप्त करें

    C# में किसी ऑब्जेक्ट के वर्तमान इंस्टेंस का प्रकार प्राप्त करने के लिए, हम GetType() मेथड का उपयोग करते हैं। यह मेथड System.Object से विरासत में मिला है और ऑब्जेक्ट के रनटाइम प्रकार को दर्शाने वाला Type ऑब्जेक्ट लौटाता है। यह रिफ्लेक्शन और डिबगिंग के लिए उपयोगी है। उदाहरण 1: स्ट्रिंग का प्रकार प्र

  1. - C# में Enum.GetType(): एन्यूमरेशन का प्रकार प्राप्त करना

    GetType() मेथड किसी भी ऑब्जेक्ट का रनटाइम टाइप लौटाता है। एनम मानों पर उपयोग करके उनका एन्यूमरेशन टाइप और अंतर्निहित टाइप जान सकते हैं। उदाहरण using System; public class Demo { public static void Main() { Enum[] values = { ConsoleColor.Blue, DayOfWeek.Sunday }; Co