In this case, it does not really matter, but in cases where you use lazy loadable variables, it matters, and you get access to your property. For instance:
private Myobject _value; public Myobject Value { get { if (_value == null) _value = new MyObject(); return _value; } } private MyObject DoSomething(); {
In the case when your method failed by calling the field directly, you either process it properly or access it using the cleanup method - through your property.
It all comes down to the architecture of your application, and for this you must ask the question: what matters most from the point of view of service?
I must say that if the field is initialized correctly, and it does not cause you a headache to access it directly, then contact it directly. If this causes additional work (and therefore additional maintenance), then refer to the property.
As with anything else, weigh the pros and cons. Use your own common sense - standards wars - a ridiculous distraction. If they do not provide you with arguments that you have not thought about yourself, do not waste your breath. If you have a good reason to choose whichever path you choose, then this path is correct - it comes down to the prerogative of the designer.
My thoughts on the pros and cons are:
Transition with property:
- I can change the implementation of the method of returning this value without the need to refactor my entire class.
- I can be lazy to load the value, which means that if I never get access to it, it will never waste resources.
- I can hide all implementation details of how the value is returned in one place, without having to process it throughout my code.
Transition with a field:
- I don’t have a potential overhead resource when I need to go through the property code every time I access a value.
- I need to make sure that the value is initialized correctly on every call or handles cases where it is not.
- I can affect the value even if my property can only provide a readable interface.
So, I assume that my approach would be to use this property if I didn’t need to write the value directly, in which case I would go with a field - since my property is read-only and therefore cannot be written to.
It's just me, though - your property can be read / written, and you can decide from a design point of view that access to the field directly is okay - and that's fine too.
A trick always does something for a reason, don’t do anything blindly just because.