Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Keywords and Statements

The default keyword

Reference for the C# default keyword: the default literal, default(T) for generics, default parameter values and the default label in switch statements.

default has three jobs. As an expression, default(T) or the shorter literal default produces the default value of a type: 0 for numbers, false for bool, '\0' for char, null for reference types and nullable value types, and a struct with every field set to its default. In a switch, default: is the label that matches when no case does. In an optional parameter, = default means "the default value of the parameter's type".

The expression form matters most in generic code, where you do not know whether T is a class or a struct and still need a "nothing yet" value.

C#
int n = default;            // 0
string? s = default;        // null
DateTime d = default;       // 0001-01-01 00:00:00

Try it

C# Default values of common 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# Generics, optional parameters and switch

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#
default(Type)      // operator, any C# version
default            // literal, C# 7.1+, type inferred from context
default:           // switch statement label
parameter = default   // optional parameter
Form Meaning
default(T) The default value of T, written with an explicit type.
default literal Same, but the compiler infers T from the target (assignment, argument, return type, ?: branch).
default: The fall-back label of a switch statement. A switch expression uses the discard _ instead.

Returns: the all-zero-bits value of T. Errors: var x = default; fails with CS8716 because there is no target type to infer.

Usage notes

  • For reference types and nullable value types default is null, so default(string) is null, not "".
  • For a struct, default skips constructors and field initializers. An all-zero instance may be invalid for your domain (for example a Money whose currency is null). Design structs so the zero value is meaningful, or check for it.
  • With nullable reference types enabled, T? Method<T>() returning default is the idiom for "no value". Use [MaybeNull] or T? so callers get a warning if they ignore it.
  • In generic code, x == default is not allowed for an unconstrained T. Use EqualityComparer<T>.Default.Equals(x, default) or x is null checks.
  • default in a switch statement can appear anywhere, but it is evaluated last. A switch expression with no matching arm throws SwitchExpressionException.
  • A default literal in ?: needs the other branch to give a type: flag ? 1 : default works, flag ? default : default does not.
  • The default pointer or default(T) of a ref struct is also allowed.