Click here to Skip to main content
15,789,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why would someone use:

public string test{ get; set; }

as opposed to:

private string test;
public string Test{ 
    get{ return test;} 
    set{ test = value;} 

Just Curious.

You don't need to declare the variable, and you can't make a mistake and use the wrong one.
That's about it, really.
Share this answer
ARBebopKid 6-Oct-11 17:01pm    

If one isn't going to do any events or validation, etc. then why not just make it a public field instead of a property?

I have used the second one in places where I plan to do something like an event later. But where I haven't planned I've used fields.

Should I be making properties for all of these instead?
[no name] 6-Oct-11 17:11pm    
When using reflection you can get Properties or Fields. But design wise one may want to only get properties (for a cleaner design). In that case if they are to be exposed I would expose them via a proptery.
OriginalGriff 7-Oct-11 4:57am    
Properties are different to fields in a number of ways: the most obvious being that public properties are visible in the Properties pane of User Controls, where fields aren't.
It is considered bad practice to expose fields at all (simply because they expose the inner working of the class and make it harder to change once it is released). Yes, you could replace the field with a property at a later data, but defining the property from the start shows what you are planning on exposing, and what the user will be allowed to do with it - private setters are available which can make design interfaces a lot tidier. If you start with a public field, then any eventual property has to have a public getter and a public setter if you aren't going to break external code.

In general, if you are planning on exposing anything to the outside world, start with it as a property, preferably with a private setter and work from there.
The earlier actually makes more sence to me unless you plan on putting logic in the setter or getter.

If memory serves me right you could not do that in the earlier versions (C# 1.1). I could be mistaken though as I fled out of that framework the moment there was a clearing.

Anyways, I would do the first because as OriginalGriff said there is no mistaken. But on the other hand I would do the second if any events needed to be fired or something (e.g. INotifyPropertyChanged). But then again, your example does not have anything of the sort in it.

Oh and dont't forget less lines of code. Unless you are getting paid by lines of code (which if you are please contact me so I can forward you my resume.. I got a great system for obsufication :D), there is a serious advantage to this when looking at a larger object/system.
Share this answer
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900