0

Possible Duplicates:
C# 3.0 Auto-Properties - useful or not?
C#: Public Fields versus Automatic Properties

To cut short regular

private int pvtFOO; 
public int publicFOO 
{ 
   get { return pvtFOO; } 
   set { pvtFOO= value; } 
}

for a public property, simple

public int publicFOO { **get; set;** }

was introduced in .NET 3

But what is the use of this short-form if there is no pvtFOO involed, why not as well simply write

public int publicFOO;
Community
  • 1
  • 1
Munish Goyal
  • 1,379
  • 4
  • 27
  • 49
  • 1
    To have blocks of code formatted as code, you have to put four spaces before each line. There is a button for it as well and it looks like this: `{}`. I did that for you this time. – R. Martinho Fernandes Dec 28 '10 at 20:38
  • 1
    See http://stackoverflow.com/questions/9304/ http://stackoverflow.com/questions/1180860/c-public-fields-versus-automatic-properties http://stackoverflow.com/questions/601621/properties-vs-methods http://stackoverflow.com/questions/3218606/what-is-the-real-purpose-of-get-set-properties-in-c – BlueRaja - Danny Pflughoeft Dec 28 '10 at 20:38
  • This was not introduced in version 4. This has been here since version 3. – R. Martinho Fernandes Dec 28 '10 at 20:38
  • 2
    In short because you can change the setter without breaking binary compatibility, which you can't if you used a field. – CodesInChaos Dec 28 '10 at 20:40
  • A more closely related link: http://stackoverflow.com/questions/111461/auto-implemented-getters-and-setters-vs-public-fields – nawfal Jun 04 '13 at 05:38

0 Answers0