Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The typeof and sizeof operators

Reference for the C# typeof and sizeof operators: get a Type object for a compile-time type, and the size in bytes of built-in and unmanaged types.

typeof(T) returns the System.Type object for a type that is known at compile time. It works with classes, structs, interfaces, enums, arrays, and generic types (both closed, like List<int>, and open, like List<>). It is the compile-time cousin of obj.GetType(), which asks a live object for its actual run-time type.

sizeof(T) gives the size in bytes of an unmanaged type. For the built-in numeric types, char and bool the answer is a constant and no special context is needed. For other structs you need an unsafe context.

C#
Type t = typeof(string);
int bytes = sizeof(long); // 8

Try it

C# typeof versus GetType

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# sizeof for built-in and custom types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
Type typeof(type-name)
int  sizeof(unmanaged-type)
Name Description
type-name Any type name, including void, arrays, pointer types and generic types. Type arguments may be omitted for an open generic (typeof(Dictionary<,>)).
unmanaged-type A type with no reference-type fields at any depth: built-in numeric types, char, bool, enums and structs that contain only such fields.

Returns: typeof returns a Type (the same instance every time within one app). sizeof returns an int.

Errors: typeof cannot take a variable or a type parameter's value (you can use typeof(T) with a generic parameter T, though). sizeof of a reference type or a struct holding references is a compile error (CS8500/CS0233). sizeof(Pair) outside an unsafe context fails with CS0233.

Usage notes

  • sizeof of the built-in types is a constant: byte/sbyte/bool are 1, short/ushort/char are 2, int/uint/float are 4, long/ulong/double are 8 and decimal is 16. bool takes a full byte, not a bit.
  • For a custom struct, sizeof reports the size the CLR uses, including padding. Unsafe.SizeOf<T>() does the same without unsafe. Marshal.SizeOf<T>() reports the size of the native (marshaled) layout, which can differ.
  • Use typeof when you need an exact type object for comparison (x.GetType() == typeof(Dog)) or as an argument to reflection, serialization and dependency-injection APIs. Use is when subclasses should match too.
  • typeof(T) inside a generic method is resolved at run time for each constructed type, and is cheap and cached.
  • obj.GetType() throws NullReferenceException on null; typeof never fails.
  • typeof(void) is valid and returns the Type for System.Void, which you meet when reflecting on method return types.
  • To get the name of a type as a string without loading a Type, prefer nameof.