views:

276

answers:

12
private List<Date> _dates;

public List<Date> Dates
{
    get { return _dates; }
    set { _dates = value; }
}

OR

public List<Date> Dates
{
    get;        
    set;    
}

I have always used the former, is that incorrect or bad practice? It never occurred to me that I could just use the second option. I do like having my encapsulated variables to begin with an underscore so I can distinguish them from method parameters. And I've just always done it that way.

Is it possible that using the first option would result in an extra List<Date> object being instantiated and then the whole of _dates being replaced with value, or is it more intelligent than that?

Also, which is the most prominent in industry or is it totally subjective?

+7  A: 

Both are basically the same because of how .NET compiles automatic properties. Automatic properties became available with .NET 3.5.

Yuriy Faktorovich
3.0 actually http://msdn.microsoft.com/en-us/library/bb384054.aspx
Chad
@Chad C# 3.0, but .NET 3.5.
Yuriy Faktorovich
@Yuriy, touche! I always forget the version numbers are out of sync
Chad
+14  A: 

Use the former if you need to add some kind of logic to your getter/setters.

Use the latter otherwise. It makes things much cleaner. You can also achieve read-only properties using auto properties:

public List<Date> Dates { get; private set; }

Or, if you don't want people to add any items to the list through the property you can resort to the former syntax:

private List<Date> _dates = new List<Date>();
private ReadOnlyCollection<Date> _readOnlyDates =
    new ReadOnlyCollection<Date>(_dates);

public ReadOnlyCollection<Date> Dates
{
    get { return _readOnlyDates; }
}
Justin Niessner
I wouldn't keep returning a new `ReadOnlyCollection`. Internally it keeps a reference to the list so any changes will show up.
ChaosPandion
The point is that callers will not be able to modify it.
recursive
I would not call an auto property with a private setter 'read-only': it can still be changed from within the class in which it is defined, i.e. it is not synonymous to the 'readonly' keyword on fields.
Paul Ruane
@Paul - To the lazy programmer that is good enough. :)
ChaosPandion
I have to say, I have never seen that used 'in the wild'. Thanks for the answers.
rmx
I really think this is the better answer. Not only does it cover standard practice, it also covers best practice by suggesting they use `ReadOnlyCollection<Date>` instead of exposing the underlying `List<Date>`.
ChaosPandion
@Chaos, Frank answered my question more directly and gave a comparison of the two examples. which is exactly what I wanted.
rmx
A: 

It doesn't matter. The second is just sugar. I always use the second because it's cleaner, but every once in a while I need access to the backing value.

Joel
A: 

As long as you don't have to make sure the List<Date> is initialized in the getter you are fine with both versions. I just suggest that you are using one version and you don't mix it in your code...

If you need it initialized you have to go for the first version...

private List<Date> _dates; 

public List<Date> Dates 
{ 
    get { if (_dates == null) _dates = new List<Date>(); return _dates; } 
    set { _dates = value; } 
} 
Yves M.
This is silly. Just do "private List<Date> _dates = new List<Date>()" or initialize in the constructor. I doubt lazy-initializing this simple list will buy anything other than more complex code.
siride
@siride - I agree, now if you were initializing a list from a database that is a whole different story.
ChaosPandion
A: 

I believe they compile to the same code. The latter is just syntactic sugar to represent the former. I tend to think of the latter as a placeholder for future logic that may need to be placed in the properties, at which point I'd convert said affected properties to the former.

David
A: 

The two are compiled roughly the same way (I don't know that the backing field's name would specifically be the same, but they'd be functionally equivelant).

IIRC, the ability to do Automatic Properties (public List<Date> Dates {get; set;}) was added in either .NET 3.0 or 3.5.

AllenG
+4  A: 

I tend to use auto properties more because a property is a good way to express intent about what your object is doing, but still have some protection.

Almost all of my property declarations look like:

public bool IsBlah { get; private set; }

This makes it a nice, readable, and protected getter.

But there are times when you want an explicit backing field:

private bool? _isBlah;
public bool IsBlah
{
    get
    {
        if (_isBlah == null)
        {
            // Do something expensive here and set _isBlah
            _isBlah = true;
        }
        return _isBlah;
    }
}
ecoffey
A: 

The former is the original approach, the latter is an example of the newer 'auto properties' facility whereby the compiler generates a backing field for you automatically.

Some people (myself included) shy away from auto properties because the syntax is easy to mistake for abstract properties, there is no facility for 'readonly' properties and the syntax for auto properties with private setters is clumsy:

public List Dates
{
    get;
    private set;
}

I also find it uncomfortable to have my classes' internal implementation accessing fields via the class API.

Paul Ruane
`I also find it uncomfortable to have my classes' internal implementation accessing fields via the class API.` Exactly! I will stick to what I use. Thanks
rmx
+4  A: 

They are equivalent in the internal compiled form, except that you cannot access the compiler generated private variable in the second form.

From an code efficiency point of view, they are equivalent as well, the just in time compiler normally directly accesses the private variable without the overhead of calling an access function (after the runtime environment has checked accessibility etc.).

From a coding perspective, I prefer the second version which is more compact (less to write, less to read).

The second syntax was introduced in C# 3.0. So the first variant would be more compatible to old compilers.

Frank
Good info regarding backward compatibility and compiled code, thanks.
rmx
+1  A: 

The second variation is know as auto-implemented properties and was introduced in C#3.0 (hence why you may not have encountered it before).

It is preferable to use this format if you want to create simple properties with backing-fields and don't need to implement any logic in the 'getter' and 'setter'. Not only is it more concise, but it also forces you to access the property directly, rather than going through the backing field, which is normally best practice.

Note you can still initialise properties, you just do it via the constructor.

public class MyClass
{

  public List<Date> Dates
  {
      get;        
      set;    
  }

  public MyClass()
  {
      Dates = new List<Date>();
  }

}
Dan Diplo
A: 

As far as I can remember, I have always managed my list properties from within my object, making them readonly.

private IList<DateTime> _dates;

public MyClass() {
    _dates = new List<DateTime>();
}

public IList<DateTime> Dates {
    get {
        return _dates;
    }
}

By doing so, I assure that my list is never null when I access it, since the client cannot assign it.

However, for your example, you only need to use the former approach if you need some kind of logic when getting or setting a property. Otherwise, automatic properties does exactly the same you would, only getting and setting a value to a private field. Furthermore, the { get; set; } improves readability and helps other programmers understand your intention, in my humble point of view.

For instance:

public class MyClass {
    private IList<DateTime> _dates;

    public IList<DateTime> Dates {
        get {
            return _dates;
        } set {
            _dates = value;
        } 
    }
}

versus

public class MyClasse {
    public IList<DateTime> Dates { get; set; }
}

The second way makes it leaner and swifter, at least to me, what the intention is. And it becomes faster to code a class, when no logic inside the getter and setter is necessary.

Otherwise, one is not better than the other and does quite the same, speaking of getting and setting simply the property's value with no logic.

Most importantly, you need to be comfortable with your code. If it ends for you not getting any improvements, and you like what you're doing, than just do it. =)

Will Marcouiller
You can add `private set` to your last example to get the same result with cleaner code. But also keep in mind handing out a reference to `IList` means it's not read only, as `IList` supports `Add`, `Remove`, `Clear`, etc. Granted, if your caller really wants to alter the list, they will do it no matter what you hand them.
Matt Greer
@Matt Greer: Yes, indeed! That is generally what I need, meaning allowing one to Add() or to Remove() from the list, otherwise I would go for a higher hierarchy level object. In addition to it, the capability of C# to have defined an accessor scope for the getter, and another for the setter, makes it very flexible to work with. That is one thing amongst many others that I miss from working with C#, as I'm currently working in VB.NET 2.0.
Will Marcouiller
A: 

I personally prefer the first method as it allows you to perform a few operations prior to the return.

E.G. (A really poor example)

    private int _limit;
    private int _onHand;
    private bool returnToVendor;

    public Item(int limit, int onHand)
    {
       _limit = limit;
       _onHand = onHand;
    }

    public int ReturnStock
    {
       get
       {
         if(_onHand > _limit)
         {
            returnToVendor = true;
            return _onHand;
         }
       }

       set
       {
           _onHand = value;

           if(_onHand < _limit)
           {
              returnToVendor = false;
           }
       }
    }
Jamie Keeling