0

I am creating a simple User class, does it matter if I use public properties with private fields verses just using public fields?

Here is an example of what I mean:

public class clsUser
{
private string name;
private string lName;

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name= value;
        }
    }

    public string LName
    {
        get
        {
            return lName;
        }
        set
        {
            lName= value;
        }
    }

public clsUser(string userID)
    {

//get the user id here and set the properties

        this.name= getName(userID);
        this.lName= getLName(userID);      
    }

}

or can I just make

public string name;
public string lName;

public and now worry about typing out all of these:

public string Name
{
    get
    {
        return name;
    }
    set
    {
        name= value;
    }
}

I am then going to populate a form using these on another page like so:

clsUser cUser - new clsUser("myid");
txtSomething.Text = cUser.name;

and so on...

I guess my question is why do I need to retype the properties first as private and then as public (as I've seen in all web examples). Why not just make them public to begin with?

Servy
  • 202,030
  • 26
  • 332
  • 449
Madam Zu Zu
  • 6,437
  • 19
  • 83
  • 129
  • "why do i need to retype the properties first as private and then as public" You don't. I think you can just write `public string Name { get; set; }` – user1477388 Feb 27 '13 at 19:30
  • Do you want people who use your class to modify the properties, if yes, then public, if not then private. – JonH Feb 27 '13 at 19:30
  • See here: [What SO says about properties versus public fields](http://www.google.com/#q=properties+vs+public+fields+site:stackoverflow.com) – Robert Harvey Feb 27 '13 at 19:30
  • 2
    [What SO's Jeff Atwood said about these issues.](http://www.codinghorror.com/blog/2006/08/properties-vs-public-variables.html) – Katie Kilian Feb 27 '13 at 19:31
  • 1
    Don't use Hungarian Notation. You class should be named `User`. – SLaks Feb 27 '13 at 19:32
  • What @SLaks just said. It will compile the way you have it, but it will be confusing to other developers who have to support your code. Recommended practice in C# is to capitalize the name of the class, and to not use Hungarian Notation (meaning, leave off the 'cls' prefix). – Katie Kilian Feb 27 '13 at 19:33
  • thank you all! i didn't realize i shouldn't be using cls for classes, thats what they taught us in school back in the day :) – Madam Zu Zu Feb 27 '13 at 19:34
  • a quick on the side question, should i not be using "btn" and "chk" for my from fields either?? – Madam Zu Zu Feb 27 '13 at 19:38
  • 1
    @TanyaXrum The code you see creates a **field** called _name_ and lets you access it through a **property** called _Name_. Since it's accessed by the property - the field can remain private. If you just use a field - Yes you can make it `public` and omit the property altogether. (As for whether you should use fields or properties - you can see the link that was posted to you question or others like it.) – ispiro Feb 27 '13 at 19:42
  • This question shouldn't have been closed. The OP here is confusing fields with properties - She is not asking why use properties instead of fields. (Note that the question was edited by someone else so now it looks as if that's the OP's question.) – ispiro Feb 27 '13 at 19:45
  • @TanyaXrum The accepted notation is to capitalize the first letter in properties but not in fields. That's what the commentators mean. – ispiro Feb 27 '13 at 19:47
  • @ispiro - thank you! i guess i just didn't understand why i need to create fields before i use them in properties, instead of just using properties from the get-go. – Madam Zu Zu Feb 27 '13 at 19:49
  • @TanyaXrum What do you mean 'get-go'? If you mean `{ get; set; }` - that's only since C# 3.0 - See [here](http://msdn.microsoft.com/en-us/library/bb384054.aspx) – ispiro Feb 27 '13 at 19:50

1 Answers1

5

You're confusing fields with properties.

String name; is a field.
Unlike a property, you have no control over it.
If you eventually decide to add validation or change events or other logic, you'll need to change it to a property, which will break compiled code.
Certain features (eg, bindings) also can only work with properties.

Instead, you can use auto-implemented properties to make the compiler generate all of that boilerplate:

public String Name { get; set; }
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964