I understand the advantages of PROPERTIES over FIELDS, but I feel as though using AUTO-implemented properties over MANUAL implemented properties doesn't really provide any advantage other than making the code a little more concise to look at it.
I feel much more comfortable using:
private string _postalCode;
public string PostalCode
{
get { return _postalCode; }
set { _postalCode = value; }
}
Instead of:
public string PostalCode { get; set; }
primarily because if I ever want to do any kind of custom implementation of get and set, I have to create my own property anyway backed by a private field. So why not just bite the bullet from the start and give all properties this flexibility straight away, for consistency? This really doesn't take but an extra second, considering that all you have to do in Visual Studio is click your private field name, and hit Ctrl+E, and you're done. And if I do it manually, then I end up with inconsistency in which there are SOME manually created public properties backed by private fields, and SOME auto-implemented properties. I feel much better with it being consistent all around, either all auto or all manual.
Is this just me? Am I missing something? Am I mistaken about something? Am I placing too much emphasis on consistency? I can always find legitimate discussions about C# features, and there are almost always pros and cons to everything, but in this case, I really couldn't find anyone who recommended against using auto-implemented properties.
It doesn't grant you anything extra beyond being concise. If you prefer the more verbose syntax, then by all means, use that.
One advantage to using auto props is that it can potentially save you from making a silly coding mistake such as accidentally assigning the wrong private variable to a property. Trust me, I've done it before!
Your point about auto props not being very flexible is a good one. The only flexibility you have is by either using private get
or private set
to limit scope. If your getters or setters have any complexity to them then the auto props are no longer a viable option.