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?
They are equivalent in the internal compiled form, except that you cannot access the compiler generated private variable in the second form.
From a 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.