kunal-chowdhury.com

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.

 



Kunal Chowdhury
If you have come this far, it means that you liked what you are reading (What’s new in C# 6.0? - Setting default values to Auto Properties).
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.

Don't forget to share your feedback/queries in the comment section below.