I have seen a lot of posts suggesting that private fields ought to be accessible from outside via properties (or at least get/set methods). I wonder are there any cases in which we should access our private fields inside the class by properties as well? Or should we just interact with our private fields directly thinking in terms "it only takes processing resources to approach them by properties"?
2 Answers
From a performance perspective, calling fields is faster than calling properties. So you should use fields inside the class as much as you can unless you have a need to call the property. On reasonable to call to a property from inside the class is to invoke the PropertyChanged
event for data binding which is done widely in MVVM pattern.

- 1,073
- 3
- 16
- 30
If accessing the field involves some logic, then a private property is adequate. Examples are the lazy creation of a resource, values involving calculations, properties that when they are set must alter other fields or properties.
private float _width;
private float Width
{
get { return _width; }
set {
_width = value;
_length = null;
}
}
private float _height;
private float Height
{
get { return _height; }
set {
_height = value;
_length = null;
}
}
private float? _length;
private float Length
{
get {
if (_length == null) {
_length = MathF.Sqrt(_width * _width + _height * _height);
}
return _length.Value;
}
}
This calculates the length only when required. The calculated length is cashed in the backing field _length
of the property. When either the length or the width are changed, then the cashed value is cleared to force a new calculation the next time the Length
property is read.

- 104,806
- 13
- 138
- 188