Click here to Skip to main content
14,029,429 members
Click here to Skip to main content
Add your own
alternative version

Stats

12.5K views
3 bookmarked
Posted 25 Nov 2014
Licenced CPOL

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

, 25 Nov 2014
Rate this:
Please Sign up or sign in to vote.
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, 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)

Share

About the Author

Kunal Chowdhury «IN»
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:





You may also be interested in...

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
memberMartinSmith7427-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190419.4 | Last Updated 26 Nov 2014
Article Copyright 2014 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid