7

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?

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Base33
  • 3,167
  • 2
  • 27
  • 31

5 Answers5

16

Consider both equivalent pieces of code:

private List<Configuration> _configurations;
public List<Configuration> Configurations
{
    get { return _configurations; }
    set { _configurations = value; }
}

and

public List<Configuration> Configurations { get; set; }

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:

  • Reduces code length
  • Reduces the time needed for reading and understanding a class
  • Hides useless information
  • Makes useful information easier to find

What's not to like?

Gorpik
  • 10,940
  • 4
  • 36
  • 56
  • 1
    And to add to this answer type `prop` in visual studio code editor and press TAB. This code snippet expand to auto property. – Huske Dec 21 '12 at 10:51
2

The behaviour between both codes is the same, auto properties are just syntax sugar to make properties without logic easier to read.

See the MSDN documentation for more explanations :

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors.

AlexH
  • 2,650
  • 1
  • 27
  • 35
  • When using auto properties I guess you would be less tempted to mess with the underlying private field (_configurations) in other instance methods. Reading /assigning to the underlying field often leads to code that is hard to follow – mortb Dec 21 '12 at 10:14
  • Yes exactly, if there is no logic you don't need to declare a field and a property.If you add logic later, it will be safer to use the property than the private field. – AlexH Dec 21 '12 at 10:18
1

I guess re-sharper has determined that an auto-property would be better here as your code is simply getting and setting a backing variable, in the same way that an auto-property would do.

Had re-sharper detected that your getter and setter were going more than just assigning to and reading from the backing variable, my assumption is that it would not be an issue.

I'm not a fan of re-sharper, for various reasons. I prefer using StyleCop and Code Analysis (FXCop), however it has it's benefits in that it can help make your code more readable and maintainable. That's all its trying to do here really.

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
0

If you are not doing anything clever in the properties then it simply reduces the code, and enhances the readability of the code - behind the scenes it produces the same code.

The only time you wouldn't want to do this is if you wanted to do something like this:

private readonly List<Configuration> _configurations = new List<Configuration>();
public List<Configuration> Configurations
{
    get { return _configurations; }        
}

Which I would probably recommend as the property will then never be null.

Ross Dargan
  • 5,876
  • 4
  • 40
  • 53
  • If you want it initialized, you can use an auto-property with a private setter and initialize it in the constructor. – Tom Faust Mar 20 '15 at 00:18
0

The only (temporarily?) advantage of using the old notation is that you are able to break on the set and get methods when they are hit.

Myrtle
  • 5,761
  • 33
  • 47