Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason to use auto-implemented properties over manual implemented properties?

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.

like image 623
CptSupermrkt Avatar asked Nov 14 '11 03:11

CptSupermrkt


People also ask

When can we use automatic properties?

11 Answers. Show activity on this post. Automatic Properties are used when no additional logic is required in the property accessors.

What is the point of auto-implemented properties in C#?

Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects.

What is auto-implemented property?

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property.

What is backing field in C#?

A private field that stores the data exposed by a public property is called a backing store or backing field. Fields typically store the data that must be accessible to more than one type method and must be stored for longer than the lifetime of any single method.


2 Answers

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.

like image 68
Ryan Berger Avatar answered Sep 18 '22 19:09

Ryan Berger


Auto-implemented properties are not guaranteed to keep the same backing field name between builds. Therefore, it is theoretically possible that serializing an object in one version of an assembly, and then re-serializing that same object in another assembly could cause breaking changes.

This is highly unlikely, but it is a valid concern if you're trying to maintain the ability to "swap out" version of your assemblies for newer versions.

By using manually implemented properties, you're guaranteed that the backing field never changes (unless you change it specifically).

Aside from that minute difference, an auto-property is a normal property that is implemented automatically with a backing field.

like image 33
cwharris Avatar answered Sep 20 '22 19:09

cwharris