Skip to content
Tutorials Center
My learning

Topics

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.

C#
int n = default;
string? s = default;
DateTime t = default(DateTime);

Try it

C# Defaults of the built-in 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.

C# default expressions and generics

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#
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

  • default is not new(). 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 context string s = default; hides a null behind a non-nullable type. Write string? s = default or initialize with a real value.
  • Generic code. default(T) is null for reference types and zero for value types; the T? warning above tells you when null is possible. Use where T : notnull or constraints to be explicit.
  • Optional parameters need a compile-time constant, default or new S() for structs.
  • Nullable of value types: default(int?) is null, not 0.
  • Compare against default with EqualityComparer<T>.Default.Equals(x, default) in generic code, or x is null / x == default for specific types. For structs x.Equals(default) is allowed, but == needs an operator.
  • SkipLocalsInit removes the zeroing of stackalloc buffers 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.