Floating FB popout byF5debug

What’s New in C# 6.0 – Auto Property Initializer

 

The first C# 6.0 feature which we are going to see today is Auto Property Initializer, with the previous versions normally we declare and initialize auto properties separately with a must constructor to create objects. But with the latest version we can Auto initialize the property without any explicit constructor declaration. Hearing it quite new, yes this feature is introduced to avoid having an object to initialize the property, rather do it while declaration itself. Let’s see how we can do that programmatically. First lets see a quick look on how we initialize properties with the first few versions of C#.Netimage

Later with the upcoming versions we still have some enhanced feature where we put the set and get without separate initializers but still we should require a Constructor to initialize the property and its mandatory as shown below. Visual Studio have a short cut keys to create properties if you are not aware of, just type “prop” and press the tab button twice which create a default property code which you can customize it as per the requirement. This will be handy when creating a multiple properties for the class object.

image

With the newer version C# 6.0 its further enhanced and now we can set the property initializers while declaring itself without having a constructor to implement with the values. So in simple we can term now properties as well like variable fields where we can assign the property with the values while declaration itself without any separate code inside the constructor as shown below. This Auto Property initializer will be more useful for data properties which will carry data with out any specific methods or function implementation.

image

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter