C# 6 new features: Auto assigning property.
C# 6 new features : Auto assigning property.
We have lots of new features and keyword added in C# 6 but among those new property auto assigning property is one of them . So here we will discuss on this particular features only.You can get this features in visual studio 2015 or higher version of Visual studio .Auto assigning property means we can set the values of property while we will declare the property. Before C# 6.0 we assigning the values to the property usually by declaring a constructor. So to improve the coding productivity the Microsoft Team added this new features in C# 6.0 .But one thing I just want to tell you that in both the cases we will get the same values but the way of initializing will be different.
So lets take an example here.
How property works in C# 5.0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | namespace Console { public class Program { // Just created the Property here private string FirstName { get;set;} private string LastName { get; set; } Program(string fName, string lName) { FirstName = fName; LastName = lName; } public static void Main(string[] args) { Program objProg = new Program("Demo", "app"); System.Console.WriteLine(objProg.FirstName); System.Console.WriteLine(objProg.LastName); System.Console.ReadKey(); } } } // Here the output will be // Demo // app |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | namespace Console { public class Program { // Just created the Property and assigning here private string FirstName { get; set; } = "Demo"; private string LastName { get; set; } = "app"; public static void Main(string[] args) { Program objProg = new Program(); System.Console.WriteLine(objProg.FirstName); System.Console.WriteLine(objProg.LastName); System.Console.ReadKey(); } } }// here the output will be "Demo" and "app". Same as above |
So in Auto assigning property features we can assign the default values to the property without declaring the constructor like C# 5.0.
Like this features we have very interesting features in C# 6.0 . We will discuss it in next Blogs.
Comments
Post a Comment