C# Tips

C# Tip Article

Primitive types in C#

In CLR, the primitive types are Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, and Single. And there are corresponding C# keyword for the primitive types as follows.

CLR Primitive C# keyword
Boolean bool
Byte byte
SByte sbyte
Int16 short
UInt16 ushort
Int32 int
UInt32 uint
Int64 long
UInt64 ulong
Char char
Single float
Double double
IntPtr N/A
UIntPtr N/A

To check out whether the object type in question is primitive or not, use Type.IsPrimitive property.

// example1
var x = GetData();
bool b = x.GetType().IsPrimitive;

// example2
bool b = typeof(decimal).IsPrimitive; // false


It is worth noting that there are some types that appear to be primitive types but are not, say for example, decimal, DateTime, or string.

In C#, primitive types are predefined struct (value) types, so string is not primitive since it is a reference type. (Of course not all built-in .NET value types are primitive types.)

C# decimal is an alias for System.Decimal, as you can see below, Decimal is a struct that consists of many primitive type (int) fields. DateTime also is based on UInt64 data field, and is not considered as primitive type.

public struct Decimal
{
	private int flags;
	private int hi;
	private int lo;
	private int mid;
}