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.
Type t = typeof(string); int bytes = sizeof(long); // 8
Try it
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.
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
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
sizeofof the built-in types is a constant:byte/sbyte/boolare 1,short/ushort/charare 2,int/uint/floatare 4,long/ulong/doubleare 8 anddecimalis 16.booltakes a full byte, not a bit.- For a custom struct,
sizeofreports the size the CLR uses, including padding.Unsafe.SizeOf<T>()does the same withoutunsafe.Marshal.SizeOf<T>()reports the size of the native (marshaled) layout, which can differ. - Use
typeofwhen you need an exact type object for comparison (x.GetType() == typeof(Dog)) or as an argument to reflection, serialization and dependency-injection APIs. Useiswhen 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()throwsNullReferenceExceptiononnull;typeofnever fails.typeof(void)is valid and returns theTypeforSystem.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.