Take a look at this code.
public struct Customer
{
private int _id;
private string _name;
public int Id
{
get { return this._id; }
set { this._id = value; }
}
public Customer(int Id, string Name)
{
this.Id = Id; // Error - CS0188: The 'this' object cannot be used before all of its fields are assigned to.
this._name = Name;
}
}
` Why do I get this error on the above line.
Now look at this code.
public struct Customer
{
private string _name;
public int Id { get; set; }
public Customer(int Id, string Name)
{
this.Id = Id; // No error recieved.
this._name = Name;
}
}
In the above code I implemented Id as an Auto-Implemented property. This time I don't get any errors. Why?
Just to make it clear following link does not have my answer.