RE: Naive question about C# Class properties

Auto-Implemented Properties simply cut down on the verbosity of a code file and make property declaration more concise when no additional logic is needed on get or set accessors. The compiler deals with the creation of anonymous private fields behind those properties.

Hope that helps...

Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"Terry Pratchett

RE: Naive question about C# Class properties

In your first example, you are not directly setting the value of a field, (member variable), you are providing a get and set accessor to said field, (member variable), in the form of a property. The property is an interface to the implementation of a field.

In the second example you are providing direct access to a field, (member variable), named 'FromAddress'.

With regard to c# there are several differences between a publically exposed field, (private member variable), and a property;

Exposing a field loses binary compatability and will cause issues with serialization

Exposing a field loses source compatibility. I.E., You can use a a field for ref parameters, whereas you can't use a property in the same way

You have fine-grained access control with properties

Properties are used for data binding, fields aren't

There other reasons as well. For example its always worth considering what you are exposing in that a property encapsulates the concept suggesting a value being made available to the outside world. Realistically this comes down to the concept of exposing an interfaces and not the implementation of that interface where a property is the interface to the implementation of a field.

Hope that makes some kind of sense...

Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"Terry Pratchett