What’s new in C# 6.0? - Setting default values to Auto Properties

Did you try the new Visual Studio 2015 and .NET 4.6? At the time of writing this blog post, it’s still in “Preview” release for the developers to try and report issues. .NET 4.6 comes with C# 6.0 with additional new features expected by developers to improve their coding productivity.


We are now going to describe the new features introduced in C# 6.0. Today in this blog post, we are going to see how to initialize/set default values to auto properties introduced in C# 6.0. Keep reading.



If you didn’t yet download Visual Studio 2015 and .NET 4.6, download it now to try out the new features introduced in C# 6.0.


For quite a long time, it was asked by the developers to initialize or set default values to auto properties where it has been declared. Earlier to C# 6.0, we had to make them as full property with a private variable to initialize at the time of declaration. But, time has changed now. You don’t need to create the full property in order to initialize them. Less code, less chances of bugs and better chances of productivity. Isn’t it? A typical auto property looks similar to this:


A typical Auto Properties (www.kunal-chowdhury.com)


But now it has changed a bit. Now you can assign default value to the auto properties without writing a second line. Let’s see, how to do it. It’s very simple. Just place an “=” equal sign at the end and write the default value to it.


Here is how you can set default value to auto properties at the time of declaring them:


Employee Class - Initialization of Auto Properties (www.kunal-chowdhury.com)

Now when you create the instance of the class, it will automatically set the default value to the properties where you set it at the time of declaration. And when you call the properties, you will see the default value the property returns if not reinitialized. Here is a small piece of code to demonstrate it:


Calling the values (www.kunal-chowdhury.com)

And when you compile and run the above code snippet, you will see that the default value returns to the output screen as the properties were not re-initialized.


Output (www.kunal-chowdhury.com)


Did you like the feature? What’s your opinion about it? Drop a line below with your comments. Don’t forget to share this link in your network and help your friends/colleague know about it.


Subscribe to my blog’s RSS feed and Email Newsletter to get the immediate update about new blog posts. Don’t forget to subscribe to my blog’s page on Facebook and connect with me on Twitter, to read what I share over the day.


More posts are coming shortly on the topic “What’s new in C# 6.0?”. Till that time, happy coding.


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. If this work the way as you describe, I'll be delighted! Usually I go get{if(_back==null) _back=defaultValue();return _back;}, which us cumbersome and darn inconvenient. This should be implemented in v5.0 or even v4.0. (To be honest, I always think that cool features should've been implemented in an earlier version, hehe.)

    1. you could at least have used get { return _back ?? _back = defaultValue(); }

    2. Yea, you could but think again which is better!!! :)

    3. @Kunal - Which is better? The clearer way - i.e. not this new way. As Jorge says its "dirty"

  2. Replies
    1. Justify the dirty comment... I'd argue the reverse - it's much clearer what it's doing than setting the value in the constructor.

    2. It isn't dirty, it'll make the code much cleaner in fact.

    3. This "new" way is a botch.

    4. I too agree with others. It's not a dirty way to code but helps you to keep your code clean.

    5. Yeah, but very helpful, maybe like this would have been better

      public string Name { get; set; default: "" }

  3. all new features introduced in http://www.kunal-chowdhury.com/ ; against their simplicity, they are extremely useful and helpful. writing an if statement to check if an expression is null or not, was really ornery. Also passing a string parameter was really a bug prone, and string concatenation using old ways was a bit time consuming and hard to read(in case of string.format). I think VS 2015, specifically C# 6.0 is going to be the most desirable one for .net developers

  4. Its really a great change in in C# by MicroSoft while Credit goes to Kunal ch. that he has explained it so easily that a very junior level programmer can also understand it clearly, Thanks MicroSoft, Thanks Kunal G

  5. I too accept Farhad point.
    You explained nicely Kunal.

  6. Yes, Its really helpful. At the time of inserting some values to db, it is going to be very helpful as we are using
    model classes.

  7. This comment has been removed by a blog administrator.


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