Open In App

How to get the typecode for enum in C#?

Last Updated : 23 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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());
    }
}


Output:

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

Reference:


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads