default value expression provides a default value for a type and are very handy in generator functions and generic classes/methods.
This is how we can use default keyword till C# 7.0 i.e. as default(T) where T can be value type or reference type
private void btnDefaultExpressions_Click(object sender, EventArgs e) { string str = default(string); //Old way of defining default strings var intValue = default(int); string stringValue = default(string); var nullableIntValue = default(int?); }
C# 7.0 enhanced default keyword by introducing null value in case as explained in my article here
style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-5021110436373536"
data-ad-slot="9215486331">
C# 7.1 enhanced default value expressions by removing the need to pass T as parameter while finding the default value of type. The Type is now inferred rather than passed as argument.
So above samples can be simplified in C# 7.1 as
private void btnDefaultExpressions_Click(object sender, EventArgs e) { string str = default(string); //Old way of defining default strings var intValue = default(int); string stringValue = default(string); var nullableIntValue = default(int?); str = default; //Not Supported by default in C# 7.0 but supported in C# 7.1 intValue = default; stringValue = default; nullableIntValue = default; }
All code samples are available at my GitHub profile
I am a corporate trainer and solution architect and have a great team of software professionals. Please contact in case you need our services by sending email to contact@techprocompsoft.com. Here is our company website .