C# 7.1’s default value expressions enhancement: default literal expressions

For using C# 7.1, set Language Version to “C# latest minor version(latest)” or “C# 7.1”  in C# project (Advanced Build Settings):

C# 7.1 default

default literal expressions and type inference in C# 7.1

default literal can be used when type of the expression can be inferred. It can be used in:

variable initialisation & assignment

optional parameter – default value declaring

method call argument

return statement

Full sample code to explain the use of default value expressions in older versions of C# (before C# 7.1)

Note the use of default value expressions:

  1. variable initialisation & assignment (errorMessage = default(string) )
  2. optional parameter – default value declaring (TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultVaule = default(TValue)))
  3. method call argument (GetBooksAvailableForDisplay(default(Func<Book, bool>)))
  4. return statement (return default(T))

in the below sample code:

Full sample code to explain the use of C# 7.1’s default literal expressions

Note the use of default literal expressions:

  1. variable initialisation & assignment (errorMessage = default )
  2. optional parameter – default value declaring (TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultVaule = default))
  3. method call argument (GetBooksAvailableForDisplay(default))
  4. return statement (return default)

in the below sample code:

Happy Coding !!!

Kapil Khandelwal

Web Developer, Blogger, Microsoft Certified Professional (MCP), DZone’s Most Valuable Blogger (MVB)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *