C# TypeCode Enum

Beginner
5 min read
Updated: Nov 2026
3 examples
Enum

Definition

In C#, TypeCode is an enum that gives a short name to each basic .NET type, such as Int32, Double, and String.

It is handy when you hold a value as object and want to switch on its type without writing many is checks.

Syntax

There are two common ways to get a TypeCode.

C#
TypeCode a = Type.GetTypeCode(typeof(int));
TypeCode b = "hello".GetTypeCode();
CallWhat it does
Type.GetTypeCode(typeof(int))Gets the code from a Type.
value.GetTypeCode()Gets the code from a built-in value, through IConvertible.

String has its own guide: String.GetTypeCode().

Members

MemberValueType it stands for
Empty0A null reference.
Object1Any type that is not listed below.
DBNull2A database null value.
Boolean3bool
Char4char
SByte, Byte5, 6sbyte, byte
Int16, UInt167, 8short, ushort
Int32, UInt329, 10int, uint
Int64, UInt6411, 12long, ulong
Single, Double13, 14float, double
Decimal15decimal
DateTime16DateTime
String18string

C# Examples

Example 1 — GetTypeCode() on Values

Ask built-in values which TypeCode they have.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine("hello".GetTypeCode());
        Console.WriteLine(42.GetTypeCode());
        Console.WriteLine(3.14.GetTypeCode());
    }
}

How It Works

  1. String value. "hello" reports String.
  2. Whole number. 42 is an int, so Int32.
  3. Decimal literal. 3.14 is a double by default.

Example 2 — Type.GetTypeCode() and Numbers

Get a code from a type. Casting a TypeCode to int shows its number.

C#
using System;

class Student { }

class Program {
    static void Main() {
        Console.WriteLine(Type.GetTypeCode(typeof(bool)));
        Console.WriteLine(Type.GetTypeCode(typeof(Student)));
        Console.WriteLine((int)TypeCode.Int32);
    }
}

How It Works

  1. Built-in type. bool gives Boolean.
  2. Your own class. Student is not a basic type, so it gives Object.
  3. Enum value. (int)TypeCode.Int32 prints 9.

Example 3 — Switch on a TypeCode

Decide whether an object holds a number.

C#
using System;

class Program {
    static bool IsNumber(object value) {
        switch (Type.GetTypeCode(value.GetType())) {
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.Double:
            case TypeCode.Decimal:
                return true;
            default:
                return false;
        }
    }

    static void Main() {
        object[] items = { 5, "text", 3.5, true };

        foreach (object item in items) {
            Console.WriteLine($"{item}: {IsNumber(item)}");
        }
    }
}

How It Works

  1. Find the type. value.GetType() gives the real type of the object.
  2. Get its code. Type.GetTypeCode() turns it into an enum value.
  3. Switch. Number codes return true. Everything else returns false.

Frequently Asked Questions

It is an enum that names the basic .NET types, such as Int32, Double, String, and Boolean.
Call value.GetTypeCode() on a built-in value, or Type.GetTypeCode(typeof(T)) for a type.
TypeCode.Object. TypeCode only describes basic built-in types.
It is common in conversion code and in switch statements over values stored as object.
Use is for most checks. TypeCode is handy when you want one switch over many numeric types.

Next: IConvertible

Learn the interface that powers GetTypeCode() and conversions.

IConvertible tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful