Type.GetTypeCode() Method in C#

The Type.GetTypeCode() method in C# is used to get the underlying type code of the specified Type. It returns a TypeCode enumeration value that represents the type classification of the given Type object.

Syntax

Following is the syntax −

public static TypeCode GetTypeCode(Type type);

Parameters

  • type − The Type object whose underlying type code is to be retrieved.

Return Value

Returns a TypeCode enumeration value that represents the type category. If the type is null, it returns TypeCode.Empty.

Using GetTypeCode() with Built-in Types

Example

using System;

public class Demo {
    public static void Main() {
        Type type1 = typeof(double);
        TypeCode type2 = Type.GetTypeCode(type1);
        Console.WriteLine("TypeCode = " + type2);
    }
}

The output of the above code is −

TypeCode = Double

Using GetTypeCode() with Different Data Types

Example

using System;

public class Demo {
    public static void Main() {
        Type type1 = typeof(short);
        TypeCode type2 = Type.GetTypeCode(type1);
        Console.WriteLine("TypeCode = " + type2);
    }
}

The output of the above code is −

TypeCode = Int16

Using GetTypeCode() with Multiple Types

Example

using System;

public class Demo {
    public static void Main() {
        Type[] types = { typeof(int), typeof(string), typeof(bool), typeof(decimal), typeof(char) };
        
        foreach (Type type in types) {
            TypeCode typeCode = Type.GetTypeCode(type);
            Console.WriteLine($"Type: {type.Name} => TypeCode: {typeCode}");
        }
        
        // Testing with null
        TypeCode nullTypeCode = Type.GetTypeCode(null);
        Console.WriteLine($"null => TypeCode: {nullTypeCode}");
    }
}

The output of the above code is −

Type: Int32 => TypeCode: Int32
Type: String => TypeCode: String
Type: Boolean => TypeCode: Boolean
Type: Decimal => TypeCode: Decimal
Type: Char => TypeCode: Char
null => TypeCode: Empty

Common TypeCode Values

C# Type TypeCode
bool Boolean
byte Byte
char Char
decimal Decimal
double Double
short Int16
int Int32
long Int64
string String

Conclusion

The Type.GetTypeCode() method provides a convenient way to determine the underlying type category of a Type object. It's particularly useful for type checking, serialization scenarios, and when working with generic types where you need to identify the specific data type at runtime.

Updated on: 2026-03-17T07:04:35+05:30

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements