Possible Duplicate:
Within the Containing Class, Use Property or Field?
Take a look at the following property:
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value
}
}
Let’s say I need access to the name property and I’m in a method within the same class that this property is declared, should I use this.Name or this._name? Is it better practice or at least cleaner to use the Public member?
You could simplify this code by using auto properties:
Now you could use the
Nameproperty from within the method.Backing fields are used when you have some more complex logic in your getter/setter. Then depending on whether you need to access the field or go through the logic in the getter/setter you would use the field or the property.