0

Possible Duplicate:
C#: Public Fields versus Automatic Properties

class Person 
{
    public Person()
    {
        Name = "Default Name";
    }

    public string Name { get; set; }
}

vs.

class Person 
{
    public Person()
    {
        Name = "Default Name";
    }

    public string Name;
}

What is the advantage of the first?

If using like, public string Name { get; private set; } then I understand it. And yes- I understand why to use properties in object oriented programming.

(and I know also in run-time there will be an private instance of name - but I cannot see the big difference - if you just using it like public string [variable] { get; set; })

Community
  • 1
  • 1
Niklas
  • 1,753
  • 4
  • 16
  • 35
  • I think simple databinding wants to have get and set. – Nickolodeon Nov 22 '11 at 22:36
  • http://www.codinghorror.com/blog/2006/08/properties-vs-public-variables.html – manojlds Nov 22 '11 at 22:38
  • @manojlds: I don't know if Jeff would still think today (the post is rather old) the same think but I disagree with most parts of his post. Another reading: http://csharpindepth.com/articles/chapter8/propertiesmatter.aspx – Otiel Nov 22 '11 at 22:55

1 Answers1

0

I often start a property with empty { get; set; } if I'm going to later code up full getter/setter methods. It lets me get the outline of my code done quickly, and fill in the details later.

I also vaguely remember some data-binding code that only worked on properties, and not on public members. In that case, empty { get; set; } will expose the property to the data-binding.

abelenky
  • 63,815
  • 23
  • 109
  • 159