Default expressions
A new expression introduced in C# 7.1 that is default literal. With the introduction of this new literal, the expression can be implicitly converted to any type and produces result as default value of the type.
Note
New default literal is different than old default(T)
. Earlier default
convert the target type of T
but newer one can convert any type.
Following is the code-snippet that is showing both old and new default
:
//Code removed case 8: Clear(); WriteLine("C# 7.1 feature: default expression"); int thisIsANewDefault = default; var thisIsAnOlderDefault = default(int); WriteLine($"New default:{thisIsANewDefault}. Old default:{thisIsAnOlderDefault}"); PressAnyKey(); break; //Code removed
In the preceding code when we are writing int thisIsANewDefault = default;
an expression that is valid in C# 7.1 and it implicitly convert the expression to type int and assign a default value that is 0 (zero) to thisIsANewDefault
. The notable point here is...