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)
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.