To encapsulate, and that way you make how you store data within your class an implementation detail. If your class has a getter/setter:
private string _mystring = string.Empty;
public string SomeValue
{
get
{
return _mystring;
}
}
That allows you to change how you manage "SomeValue" in the future. For example, you may decide that rather than storing it in a string, it's actually going to be a Guid under the hood. Now you could go for:
private Guid _myguid= Guid.NewGuid();
public string SomeValue
{
get
{
return _myguid.ToString();
}
}
Anywhere that your class is used and the "SomeValue" property is accessed will remain unbroken this way.
Validation
You can also validate the value passed in as well, so in another example:
private string _mystring = "My Value";
public string SomeValue
{
get
{
return _mystring;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new InvalidArgumentException();
}
else
{
_myString = value;
}
}
}
To ensure that the stored string is never set to a null/empty string