Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Naive question about C# Class propertiesHelpful Member!(3) 

ChewDoggie (Programmer)
30 Aug 11 10:59
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.

Helpful Member!(3)  Rhys666 (Programmer)
30 Aug 11 11:14
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

ChewDoggie (Programmer)
30 Aug 11 12:01
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.

jmeckley (Programmer)
30 Aug 11 12:47
this feature was added with .net 3.0, 3.5 along with the var keyword and lamda syntax.

Jason Meckley
Programmer

FAQ855-7190: Database Connection Management
FAQ732-7259: Keeping the UI responsive

Qik3Coder (Programmer)
31 Aug 11 18:37
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!

Rhys666 (Programmer)
1 Sep 11 3:32
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;
  1. Exposing a field loses binary compatability and will cause issues with serialization
  2. 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
  3. You have fine-grained access control with properties
  4. 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

jmeckley (Programmer)
1 Sep 11 8:11
Qik3Coder (Programmer)
1 Sep 11 10:43
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!

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close