The following table shows the default values of C# types:
Default value expressionsUse the default
operator to produce the default value of a type, as the following example shows:
int a = default(int);
You can use the default
literal to initialize a variable with the default value of its type:
int a = default;
Parameterless constructor of a value type
For a value type, the implicit parameterless constructor also produces the default value of the type, as the following example shows:
var n = new System.Numerics.Complex();
Console.WriteLine(n); // output: (0, 0)
At run time, if the System.Type instance represents a value type, you can use the Activator.CreateInstance(Type) method to invoke the parameterless constructor to obtain the default value of the type.
Note
A structure type (which is a value type) may have an explicit parameterless constructor that may produce a non-default value of the type. Thus, we recommend using the default
operator or the default
literal to produce the default value of a type.
For more information, see the following sections of the C# language specification:
See alsoCollaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide. Additional resources In this articleWas this page helpful?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4