By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 2,007 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

Auto Property in C# 6.0

P: 12
The auto property came into existence at C# version 3.0. It is used for code simplification and by using this your code becomes more readable
One of the great features of c# is you can declare a property.

Expand|Select|Wrap|Line Numbers
  1. public class Student
  2.     {
  3.         public Guid Id { get; }=Guid.NewGuid();
  4.     }
  5.  
In this code, you can initialize the property at the time of declaration. By using this feature you donít have a need to write a constructor and initialize the property in that constructor.
In the previous version of c# we not able to write a property only with get.

https://imgur.com/AudqH3t


Above image shows it will give an error in c# version 5.0 and below but if you declared read-only property in c# it will not throw an error. Below is the image of version 6.0

https://imgur.com/ZtQdhGM
4 Weeks Ago #1
Share this Article
Share on Google+