C# Feature - default literal
Overview
Section titled “Overview”The default literal allows default to stand on its own when the target type is already known from context.
Introduced In
Section titled “Introduced In”C# 7.1 (2017)
Before
Section titled “Before”CancellationToken token = default(CancellationToken);CancellationToken token = default;Annotated Example
Section titled “Annotated Example”public void Reset(Span<int> buffer){ for (int i = 0; i < buffer.Length; i++) { buffer[i] = default; }}Gotchas & Best Practices
Section titled “Gotchas & Best Practices”- 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.
Related Features
Section titled “Related Features”Version Notes
Section titled “Version Notes”- C# 7.1 introduced the default literal to reduce repetition.