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:
// 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:
- https://docs.microsoft.com/en-us/dotnet/api/system.enum.gettypecode?view=netframework-4.8sfdsfsdf