Skip to content

C# Feature - default literal

The default literal allows default to stand on its own when the target type is already known from context.

C# 7.1 (2017)

CancellationToken token = default(CancellationToken);
CancellationToken token = default;
public void Reset(Span<int> buffer)
{
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = default;
}
}
  • Use default(T) when the target type is not obvious to the reader.
  • The literal works best in assignments, returns, and optional arguments with clear typing.
  • C# 7.1 introduced the default literal to reduce repetition.