C# Convert GetTypeCode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.GetTypeCode returns a TypeCode enum value describing a runtime object or a Type.

Pair it with ChangeType or switch statements when you branch on primitive kinds.

This is a static member of System.Convert. You call it with the type name, such as Convert.GetTypeCode(...). Convert is the everyday API for converting between common CLR types in C#.

Syntax

Pass an object instance or a Type.

C#
TypeCode a = Convert.GetTypeCode(42);
TypeCode b = Convert.GetTypeCode("hi");
TypeCode c = Convert.GetTypeCode(typeof(double));
CallWhat it does
Convert.GetTypeCode(42)Returns TypeCode.Int32.
Convert.GetTypeCode("hi")Returns TypeCode.String.
Convert.GetTypeCode(typeof(double))Returns TypeCode.Double.

null yields TypeCode.Empty. Unknown objects may yield TypeCode.Object.

Library declarations
C#
public static TypeCode GetTypeCode(object? value);
// related: Type.GetTypeCode(Type type)

Parameters

ParameterDescription
valueRequired. Object whose TypeCode you want (null → Empty).

Return Value

Return ValueDescription
TypeCodeEnum describing the primitive kind.

C# Examples

Example 1 — TypeCodes for Common Values

Inspect ints, strings, and bools.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Convert.GetTypeCode(42));
        Console.WriteLine(Convert.GetTypeCode("hi"));
        Console.WriteLine(Convert.GetTypeCode(true));
    }
}

How It Works

  1. 42 → Int32. Boxed int reports Int32.
  2. Text → String. String instances map to String.
  3. true → Boolean. Bool maps to Boolean.
  4. Enum names. Console prints the TypeCode identifier.

Example 2 — Null Means Empty

See TypeCode.Empty for null.

C#
using System;

class Program {
    static void Main() {
        object? missing = null;
        Console.WriteLine(Convert.GetTypeCode(missing));
        Console.WriteLine(Convert.GetTypeCode(3.14));
    }
}

How It Works

  1. Null → Empty. Special TypeCode for missing values.
  2. 3.14 → Double. Floating literals report Double.
  3. Branch safely. Check Empty before converting.
  4. Not Object. Empty is distinct from TypeCode.Object.

Example 3 — Branch on TypeCode

Use GetTypeCode inside a switch expression.

C#
using System;

class Program {
    static void Main() {
        object value = 100;
        string kind = Convert.GetTypeCode(value) switch {
            TypeCode.Int32 => "int-like",
            TypeCode.String => "text",
            _ => "other"
        };
        Console.WriteLine(kind);
        Console.WriteLine(Convert.GetTypeCode(value));
    }
}

How It Works

  1. Switch arms. Map TypeCode to a label.
  2. Int32 path. 100 selects int-like.
  3. Print code. Confirms Int32.
  4. Extensible. Add more TypeCode cases as needed.

Frequently Asked Questions

TypeCode.Empty.
No—it only reports the kind.
Use ChangeType or a typed To* method.
Often TypeCode.Object unless they implement IConvertible specially.
Type.GetTypeCode takes a Type; Convert.GetTypeCode commonly takes an instance.

Next: IsDBNull()

Test whether a value is database null (DBNull) with System.Convert.

IsDBNull() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful