A default value expression produces the default value of a type. Two kinds of default value expressions exist: the default operator call and a default literal.
You also use the default keyword as the default case label within a switch statement.
ms.custom: "updateeachrelease"
The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.
The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.
default operator
The argument to the default operator must be the name of a type or a type parameter, as the following example shows:
sample · csharpdocs/csharp/language-reference/operators/snippets/shared/DefaultOperator.csWithOperand
namespace operators;
public static class DefaultOperator
{
public static void Examples()
{
WithOperand();
DefaultLiteral();
}
private static void WithOperand()
{
// <SnippetWithOperand>
Console.WriteLine(default(int)); // output: 0
Console.WriteLine(default(object) is null); // output: True
void DisplayDefaultOf<T>()
{
var val = default(T);
Console.WriteLine($"Default value of {typeof(T)} is {(val == null ? "null" : val.ToString())}.");
}
DisplayDefaultOf<int?>();
DisplayDefaultOf<System.Numerics.Complex>();
DisplayDefaultOf<System.Collections.Generic.List<int>>();
// Output:
// Default value of System.Nullable`1[System.Int32] is null.
// Default value of System.Numerics.Complex is (0, 0).
// Default value of System.Collections.Generic.List`1[System.Int32] is null.
// </SnippetWithOperand>
}
private static void DefaultLiteral()
{
// <SnippetDefaultLiteral>
T[] InitializeArray<T>(int length, T initialValue = default)
{
if (length < 0)
{
throw new ArgumentOutOfRangeException(nameof(length), "Array length must be nonnegative.");
}
var array = new T[length];
for (var i = 0; i < length; i++)
{
array[i] = initialValue;
}
return array;
}
void Display<T>(T[] values) => Console.WriteLine($"[ {string.Join(", ", values)} ]");
Display(InitializeArray<int>(3)); // output: [ 0, 0, 0 ]
Display(InitializeArray<bool>(4, default)); // output: [ False, False, False, False ]
System.Numerics.Complex fillValue = default;
Display(InitializeArray(3, fillValue)); // output: [ (0, 0), (0, 0), (0, 0) ]
// </SnippetDefaultLiteral>
}
}default literal
You can use the default literal to produce the default value of a type when the compiler can infer the expression type. The default literal expression produces the same value as the default(T) expression where T is the inferred type. You can use the default literal in any of the following cases:
- In the assignment or initialization of a variable.
- In the declaration of the default value for an optional method parameter.
- In a method call to provide an argument value.
- In a
returnstatement or as an expression in an expression-bodied member.
The following example shows the usage of the default literal:
sample · csharpdocs/csharp/language-reference/operators/snippets/shared/DefaultOperator.csDefaultLiteral
namespace operators;
public static class DefaultOperator
{
public static void Examples()
{
WithOperand();
DefaultLiteral();
}
private static void WithOperand()
{
// <SnippetWithOperand>
Console.WriteLine(default(int)); // output: 0
Console.WriteLine(default(object) is null); // output: True
void DisplayDefaultOf<T>()
{
var val = default(T);
Console.WriteLine($"Default value of {typeof(T)} is {(val == null ? "null" : val.ToString())}.");
}
DisplayDefaultOf<int?>();
DisplayDefaultOf<System.Numerics.Complex>();
DisplayDefaultOf<System.Collections.Generic.List<int>>();
// Output:
// Default value of System.Nullable`1[System.Int32] is null.
// Default value of System.Numerics.Complex is (0, 0).
// Default value of System.Collections.Generic.List`1[System.Int32] is null.
// </SnippetWithOperand>
}
private static void DefaultLiteral()
{
// <SnippetDefaultLiteral>
T[] InitializeArray<T>(int length, T initialValue = default)
{
if (length < 0)
{
throw new ArgumentOutOfRangeException(nameof(length), "Array length must be nonnegative.");
}
var array = new T[length];
for (var i = 0; i < length; i++)
{
array[i] = initialValue;
}
return array;
}
void Display<T>(T[] values) => Console.WriteLine($"[ {string.Join(", ", values)} ]");
Display(InitializeArray<int>(3)); // output: [ 0, 0, 0 ]
Display(InitializeArray<bool>(4, default)); // output: [ False, False, False, False ]
System.Numerics.Complex fillValue = default;
Display(InitializeArray(3, fillValue)); // output: [ (0, 0), (0, 0), (0, 0) ]
// </SnippetDefaultLiteral>
}
}C# language specification
For more information, see the Default value expressions section of the C# language specification.