I understand the need for encapsulation and the variables in a class declared as
public String VariableName{get; set; } or
private string _variableName;
public string VariableName {
get {
return _variableName;
}
set {
_variableName=value;
}
}
One of the reasons for doing this I understood is to avoid threads accessing the variable during race conditions and changing the class property. But this could not only be the reason of this encapsulation. How can other classes change a variable of this class? I think to directly access a property we would need to use the static keyword. Otherwise, the property can only be changed by creating objects of this class. Also, how does this actually encapsulate? We can access the _variableName field using the VariableName property of the object. So, essentially we still have access to it. Could someone please help me understand this concept?
TIA.