C# Reference ยท Types and Literals
Default values
Reference for C# default values: what every type starts as, default and default(T), zeroed fields and array elements, nullable types and the compiler's checks.
Every type in C# has a default value: the value you get when memory is zeroed and no code has assigned anything. For numeric types it is zero, for bool it is false, for char it is '\0', for enums it is the number zero, for structs it is every field set to its default, and for reference types and Nullable<T> it is null.
Fields of classes and structs, elements of new arrays and the slots of a new Span<T> allocated with stackalloc start at their defaults (stackalloc is zeroed unless SkipLocalsInit is applied). Local variables do not: the compiler forces you to assign them before use.
int n = default; string? s = default; DateTime t = default(DateTime);
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
T value1 = default; // default literal (C# 7.1): type from context T value2 = default(T); // default operator: explicit type var value3 = default(int); // var needs the explicit form void M(int x = default) { } // optional parameter with default
| Type | Default value |
|---|---|
byte, short, int, long (and unsigned) |
0 |
float, double, decimal |
0.0, 0.0, 0m |
bool |
false |
char |
'\0' (code 0) |
enum |
numeric 0, named or not |
struct |
every field set to its default |
| Tuple | each element set to its default |
Reference type, string, array, delegate |
null |
T? (nullable value type) |
null (no value) |
DateTime, Guid, TimeSpan |
0001-01-01 00:00:00, all zeros, zero length |
Errors. Using an unassigned local variable is a compile error (the compiler tracks definite assignment). Reading a field of a struct local that you have not fully assigned is also rejected. Fields and array elements never trigger this check because the runtime zeroes them.
Usage notes
defaultis notnew().default(S)zeros memory without running any constructor, even a parameterless one you wrote.new S()runs it. Arrays of structs and uninitialized fields use zeroing, so a struct's all-zero state must be valid.- Reference types default to
null, which is why nullable reference types matter: in an annotated contextstring s = default;hides anullbehind a non-nullable type. Writestring? s = defaultor initialize with a real value. - Generic code.
default(T)isnullfor reference types and zero for value types; theT?warning above tells you whennullis possible. Usewhere T : notnullor constraints to be explicit. - Optional parameters need a compile-time constant,
defaultornew S()for structs. - Nullable of value types:
default(int?)isnull, not0. - Compare against default with
EqualityComparer<T>.Default.Equals(x, default)in generic code, orx is null/x == defaultfor specific types. For structsx.Equals(default)is allowed, but==needs an operator. SkipLocalsInitremoves the zeroing ofstackallocbuffers and locals for speed; read nothing you did not write.- Enum default. The zero value may have no name, so give every enum a member with value 0.