3

Possible Duplicate:
Difference between Property and Field in C# .NET 3.5+

What's the difference between using

public string Username { get; set; }

and using

public string Username;

I have always been using the first one, but wanted to understand if there is any difference between the two, and scenarios when one should prefer over the other.

Community
  • 1
  • 1
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281

4 Answers4

5
public string Username { get; set; }
  • is a Property.

while

public string Username;
  • is a Public variable.

For more comparison,

  • Reflection works differently on variables vs. properties, so if you rely on reflection, it's easier to use all properties.
  • You can't databind against a variable.
  • Changing a variable to a property is a breaking change.

Other link

John Woo
  • 258,903
  • 69
  • 498
  • 492
3

One thing you can do with properties that you can't do with fields is limit visibility for either setter or getter:

public string MyProperty { get; private set; }

Something I use quite a lot.

And something (more powerful) you can't do with fields is define them inside an interface. Suppose you want an interface that requires implementing classes to have a certain property:

public interface MyInterface
{
    string MyProperty { get; }
}

Note that you do not need to have a setter here. It is entirely up to implementing classes to determine how they should set MyProperty.

Sagar Modi
  • 770
  • 2
  • 7
  • 29
1

Here is a very small example of one way you could use a string property over simply using a string.

Say you have a private variable called:

private string _name;

Now lets say you wanted to make that string read only? In other words, you can't change the value. You could use the following:

public string Name 
{
    get { return _name; }
}

It can allow you to control access to that value. Alternatively, you can have it so that that variable can only be write only doing the following:

public string Name
{
    set { _name = value; }
}

Now if you put it together, it will allow you to set to value or simply get the value. See the following:

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

You may be wondering what the point of that is since it looks like you can do the same thing with a regular string, well of course but this controls direct access to the _name variable from outside classes that aren't derived from said class.

Now what if you wanted to control how that value is set? What if you want to do some calculation or perhaps you wanted to add a prefix or suffix to that value? You do the following:

public string Name
{
    get
    {
        return _name;
    }
    set
    {
        if (value.ToLower() == "bilbo")
            _name = "Bilbo Baggins";
    }
}

Now, if you set the Name property of the class to bilbo, the value of _name will be set to Bilbo Baggins as opposed to if you set the property to Amy, the _name variable will contain simply, amy.

You can do this to guarantee that whatever value that the property is set to is automatically upper or lowercase, or perhaps you can do some validation on the value or something of that sort.

I hope this explains the uses of properties and how they can be useful without making it too complicated.

James Shaw
  • 839
  • 1
  • 6
  • 16
0

Properties provide you with more flexibility, especially in .NET. C# shows bias toward properties, so keep that in mind. However, as a general rule, use accessors/mutators when getting or setting needs "processing" or an accompanying action. Use fields for holding values. E.g.,

public class Name
{
  public string First;

  public string Last;

  public string Full{ get { return this.First + " " + this.Last; } }
} 
0x1mason
  • 757
  • 8
  • 20