I found many examples with confusing class-definitions:
variable-declaration and direct assignment, but no standard-constructor.
Are constructors needles or is this a renewal in C#6?
Example: class Table { public int[] number = new number[10];}
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Francis SusaimichaelRank #466 · 5.4K reputationPosted Dec 12, 2015, 10:44 AM EST
Rajesh SinghRank #1,806 · 824 reputationPosted Dec 17, 2015, 12:50 AM EST
1. Auto Property Initialzier
Before
The only way to initialize an Auto Property is to implement an explicit constructor and set property values inside it.
public class AutoPropertyBeforeCsharp6 { private string _postTitle = string.Empty; public AutoPropertyBeforeCsharp6() { //assign initial values PostID = 1; PostName = "Post 1"; } public long PostID { get; set; } public string PostName { get; set; } public string PostTitle { get { return _postTitle; } protected set { _postTitle = value; } } }After
In C# 6, auto implemented property with initial value can be initialized without having to write the constructor. We can simplify the above example to the following:
public class AutoPropertyInCsharp6 { public long PostID { get; } = 1; public string PostName { get; } = "Post 1"; public string PostTitle { get; protected set; } = string.Empty; }