I have seen a lot of code on github where the author will declare the member variables of a class as private, as well as prefix the name with an underscore. Then, they use a public property to provide a getter/setter for the variable.
private int _number;
public int Number
{
get { return _number; }
set { _number = value; }
}
I was taught in school to always have getters/setters for private variables, as accessing variables directly using public is a bad design practice. But if that is so, then why is this so widely used? Isn't this essentially the same as just having a public variable in the first place? What advantages does this bring?
Aucun commentaire:
Enregistrer un commentaire