Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between these two accessor/getter/setter methods?

Whats the difference now between doing this:

public string Title { get; set; }

and this:

public string Title;

Back in the day people always said use accessor methods with private variables called by the public accessor, now that .net has made get; set; statements so simplified that they look almost the same without the private variable as just using a public only variable, so whats the point and difference?

like image 584
David Avatar asked May 19 '26 07:05

David


1 Answers

I have an article on this: Why properties matter.

In short: properties are part of an API. Fields are part of an implementation. Don't expose your implementation to the world. You can change an automatically implemented property to have more behaviour (maybe logging, for example) in a source and binary compatible way. You can't do that with a field.

like image 107
Jon Skeet Avatar answered May 20 '26 22:05

Jon Skeet