Click here to Skip to main content
15,888,527 members
Articles / Programming Languages / C#

What’s New in C# 6.0? - Setting Default Values to Auto Properties

Rate me:
Please Sign up or sign in to vote.
4.92/5 (4 votes)
25 Nov 2014CPOL2 min read 15.9K   3   2
Setting Default Values to Auto Properties

Image 1Did 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.

Image 2

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, times have 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/colleagues 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.

Reference: http://www.kunal-chowdhury.com
You may like to follow me on twitter @kunal2383 or may like the Facebook page of my blog http://www.facebook.com/blog.kunal.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
QuestionI like this colorful series! Pin
jediYL30-Nov-14 16:36
professionaljediYL30-Nov-14 16:36 
QuestionYes Pin
MartinSmith7427-Nov-14 9:00
MartinSmith7427-Nov-14 9:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.