How to get TypeCode in C#?

To get the TypeCode in C#, you use the GetTypeCode() method available on all value types and some reference types. The TypeCode is an enumeration that represents the type classification of a .NET type, providing a simplified way to identify common data types.

Syntax

Following is the syntax for getting TypeCode −

TypeCode typeCode = value.GetTypeCode();

You can also use the static method from the Convert class −

TypeCode typeCode = Type.GetTypeCode(typeof(DataType));

Return Value

The GetTypeCode() method returns a TypeCode enumeration value that represents the type classification. Common TypeCode values include String, Int32, Double, Boolean, DateTime, and Object.

Using GetTypeCode() with Different Data Types

Example with String

using System;

public class Demo {
   public static void Main() {
      string s = "Demo";
      Console.WriteLine("String = " + s);
      Console.WriteLine("String Type = " + s.GetType());
      Console.WriteLine("GetTypeCode = " + s.GetTypeCode());
   }
}

The output of the above code is −

String = Demo
String Type = System.String
GetTypeCode = String

Example with Numeric Types

using System;

public class Demo {
   public static void Main() {
      int i = 100;
      double d = 5.26d;
      Console.WriteLine("Value1 = " + i);
      Console.WriteLine("GetType = " + i.GetType());
      Console.WriteLine("GetTypeCode = " + i.GetTypeCode());
      Console.WriteLine("\nValue2 = " + d);
      Console.WriteLine("GetType = " + d.GetType());
      Console.WriteLine("GetTypeCode = " + d.GetTypeCode());
   }
}

The output of the above code is −

Value1 = 100
GetType = System.Int32
GetTypeCode = Int32

Value2 = 5.26
GetType = System.Double
GetTypeCode = Double

Using Type.GetTypeCode() Method

You can also get TypeCode using the static Type.GetTypeCode() method, which is useful when working with Type objects −

Example

using System;

public class Demo {
   public static void Main() {
      bool flag = true;
      DateTime dt = DateTime.Now;
      decimal money = 250.75m;
      
      Console.WriteLine("Boolean value: " + flag);
      Console.WriteLine("Boolean TypeCode: " + Type.GetTypeCode(typeof(bool)));
      
      Console.WriteLine("\nDateTime value: " + dt);
      Console.WriteLine("DateTime TypeCode: " + dt.GetTypeCode());
      
      Console.WriteLine("\nDecimal value: " + money);
      Console.WriteLine("Decimal TypeCode: " + money.GetTypeCode());
   }
}

The output of the above code is −

Boolean value: True
Boolean TypeCode: Boolean

DateTime value: 12/6/2024 10:30:45 AM
DateTime TypeCode: DateTime

Decimal value: 250.75
Decimal TypeCode: Decimal

Common TypeCode Values

C# Type TypeCode Value
bool Boolean
int Int32
double Double
string String
DateTime DateTime
decimal Decimal

Conclusion

The GetTypeCode() method provides a simple way to determine the TypeCode enumeration value for built-in .NET types. This is particularly useful for type checking, serialization, and conversion operations where you need to identify the specific data type classification.

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

258 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements