Resharper is suggesting I change the following code to an auto-property. Can anyone explain why this would be better?
private List<Configuration> _configurations;
public List<Configuration> Configurations
{
get { return _configurations; }
set { _configurations = value; }
}
To:
public List<Configuration> Configurations { get; set; }
Why is it okay to do this to primitive types but suggests this way for object types?
Consider both equivalent pieces of code:
and
To a reader, assuming she is knowledgeable of C#, the second piece code is very quick to read. The first one takes longer and does not add any information. In fact, it adds useless information: I have a property
Configurations, but I also have an equivalent field_configurations, and the code in the rest of the class may use any of them, so I have to take them both into account. Now imagine your class has fifteen properties like this one, for instance. Using automatic properties you greatly reduce complexity for whoever is reading the code.Besides, if you consistently use automatic properties, whenever you write a non-automatic one the reader is warned immediately that there is something going on there. This useful information is hidden if you don’t use automatic properties.
In summary, consistent use of automatic properties:
What’s not to like?