Let's learn about default literal in C# 7.1

Let's learn about default literal in C# 7.1

If you are using Visual Studio 2017 version 15.3, it came with new improvements on top of C# 7.0, thus changing the version to 7.1. One of the new change in C# 7.1 is 'default' literal to avoid the redundant information while writing code.


In this post, we will learn about this improvement and will also cover how to use this. Continue reading to know about it.


Let's learn about default literal in C# 7.1


You may already know about the default(T) operator in C#. It's been used to get the default value of the specified type. The default literal in C# 7.1 is just a shorter form variation of the same, to reduce the redundant code.


The new expression, using the default literal, allows you to omit the the type and keep the code simple. But internally, it acts like default(T) only. An expression with this classification can be implicitly converted to any type, by a default-or-null literal conversion.


To know more about the C# 7.0 features, checkout my book 'Mastering Visual Studio 2017'.


Here's some examples to demonstrate how you used the default(T) operator earlier and how you can do the same using the default literal:


Old ways... using the 'default(T)' operator

Let's first see, how the old ways work, having the redundant declaration:

int count = default(int);
const string str = default(string);
ObservableCollection<Person> p = default(ObservableCollection<Person>);
List<Person> p = flag ? default(List<Person>) : GetPerson();
var person = flag ? default(List<Person>) : GetPerson();
public Person PersonDetails { get { return default(Person); } }
public void Process(List<Person> persons = default(List<Person>)) { }


New way... using the 'default' literal

Now, the same thing can be written using the new 'default' literal, which will not only reduce the redundant entries, but will also make the code clear:

int count = default;
const string str = default;
ObservableCollection<Person> p = default;
List<Person> p = flag ? default : GetPerson();
var person = flag ? default : GetPerson();
public Person PersonDetails { get { return default; } }
public void Process(List<Person> persons = default) { }


Was it clear? Do let me know, if you have any queries. To know more about the C# 7.0 features, checkout my book titled 'Mastering Visual Studio 2017'.



If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.


  1. This comment has been removed by the author.

  2. I'm not sure this saves anything. The required type needs to be specified somewhere, so if you're in the habit of using
    var x = default(IEnumerable<string>);
    you'll need to drop the 'var' and instead use
    IEnumerable<string> x = default;

    Each is just as readable/obscure as the other.


© 2008-2018 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top