views:

70

answers:

3

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?

+7  A: 

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.

Jon Skeet
A: 

In the second case, you can't modify the implementation of the accessor (because is not an accessor) without recompiling the dependent assemblies.

onof
+2  A: 

The first one

public string Title { get; set; }

is a property (Which is in fact a function).

The second one

public string Title;

Is a field.

It is good to use properties to hide the implementation (Encapsulation).

Itay
BUt there is not implmentation being hidden here?
David
Indeed there is not. But you might add one someday or will arise the need to change the internal implementation so you will be able to do it without changing the interface.
Itay