none
Type of enum in C# RRS feed

  • Question

  • Hello,

    Default type of each element in enumeration is Int. Now explicit declare long.

    How can get type element of enum? My code isn't true.

     class Program
        {
            enum Fruits : long { Apple , Orrange, Fig };
            static void Main(string[] args)
            {
                Console.WriteLine(Fruits.Fig.GetType());
    
                Console.ReadKey();
            }
        }

    Friday, February 23, 2018 12:18 PM

Answers

  • Are you looking for typeof(Fruits).GetEnumUnderlyingType() ?

    This method returns the "element type" (Int64, in your case).

    • Proposed as answer by Fei HuModerator Saturday, February 24, 2018 8:31 AM
    • Marked as answer by Arash_89 Saturday, February 24, 2018 8:43 AM
    Friday, February 23, 2018 1:13 PM
  • Hello Arash_89,

    The GetEnumUnderlyingType is good choice for you. If you have already get enum instance, you could try this.

     Fruits fruits = Fruits.Fig;         
     Type underlyingType = Enum.GetUnderlyingType(fruits.GetType());

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Arash_89 Saturday, February 24, 2018 9:05 AM
    Saturday, February 24, 2018 8:51 AM
    Moderator

All replies

  • Are you looking for typeof(Fruits).GetEnumUnderlyingType() ?

    This method returns the "element type" (Int64, in your case).

    • Proposed as answer by Fei HuModerator Saturday, February 24, 2018 8:31 AM
    • Marked as answer by Arash_89 Saturday, February 24, 2018 8:43 AM
    Friday, February 23, 2018 1:13 PM
  • Hello Arash_89,

    The GetEnumUnderlyingType is good choice for you. If you have already get enum instance, you could try this.

     Fruits fruits = Fruits.Fig;         
     Type underlyingType = Enum.GetUnderlyingType(fruits.GetType());

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Arash_89 Saturday, February 24, 2018 9:05 AM
    Saturday, February 24, 2018 8:51 AM
    Moderator