How to get the typecode for enum in C#?

Enum.GetTypeCode Method is used to get the type code of the underlying type of this enumeration member.

Syntax:

public TypeCode GetTypeCode ();

Returns: This method returns type code of the underlying type of this instance.



Exception: This method will give InvalidOperationException if the enumeration type is unknown.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// Enum.GetTypeCode() Method
using System;
  
class GFG {
      
    enum Color {Blue, Black };
  
    // Main Method
    public static void Main(String[] args)
    {
        Color c1 = Color.Blue;
        Console.Write("TypeCode of Enum Constant " + c1 + " : ");
  
        // Using the GetTypeCode() Method
        Console.WriteLine(c1.GetTypeCode());
  
        Color c2 = Color.Black;
        Console.Write("TypeCode of Enum Constant " + c2 + " : ");
  
        // Using the GetTypeCode Method
        Console.WriteLine(c2.GetTypeCode());
    }
}

chevron_right


Output:

TypeCode of Enum Constant Blue : Int32
TypeCode of Enum Constant Black : Int32

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.