Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Naive question about C# Class properties 3

Status
Not open for further replies.

ChewDoggie

Programmer
Mar 14, 2005
604
US
Hello All,

When working with properties, I'm used to seeing the following code:


Code:
public string FromAddress
{
    get { return _someaddress; }
    set { _someaddress = value; }
}

But lately I've seen some Properties defined like this:

Code:
public string FromAddress { get; set; }

Where are the values being stored in the above format ?

Thanks

Chew

10% of your life is what happens to you. 90% of your life is how you deal with it.
 
That is an Auto-Implemented Property.

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
 
TY! That helps a lot.

Chew


10% of your life is what happens to you. 90% of your life is how you deal with it.
 
this feature was added with .net 3.0, 3.5 along with the var keyword and lamda syntax.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Rhyss & JMeckley,

Have a derived question based of the OP's question.
Why would I bother writing this:
Code:
public string FromAddress { get; set; }

When I can just write this:
Code:
public string FromAddress;

Is there some value add or it just seems better?

Lodlaiden

You've got questions and source code. We want both!
 
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;
[ol]
[li]Exposing a field loses binary compatability and will cause issues with serialization[/li]
[li]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[/li]
[li]You have fine-grained access control with properties[/li]
[li]Properties are used for data binding, fields aren't[/li]
[/ol]

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
 
Rhys, well said :)

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thank you for that explanation.
I write a lot of "internal" code, that will never see the light outside these walls.
I was unaware of the nuances.

Lodlaiden

You've got questions and source code. We want both!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top