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));
Call
What 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
Parameter
Description
value
Required. Object whose TypeCode you want (null → Empty).
Return Value
Return Value
Description
TypeCode
Enum 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));
}
}
Output
Int32
String
Boolean
How It Works
42 → Int32. Boxed int reports Int32.
Text → String. String instances map to String.
true → Boolean. Bool maps to Boolean.
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));
}
}
Output
Empty
Double
How It Works
Null → Empty. Special TypeCode for missing values.
3.14 → Double. Floating literals report Double.
Branch safely. Check Empty before converting.
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));
}
}
Output
int-like
Int32
How It Works
Switch arms. Map TypeCode to a label.
Int32 path. 100 selects int-like.
Print code. Confirms Int32.
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.