String Usage Guidelines
Please read this first: About Dave’s ‘unofficial’ Framework Design Guidelines.
ý DO NOT return a null reference (Nothing in Visual Basic and nullptr in C++/CLI) from a string property; return an empty string instead.
Users of your class expect to be able to simply check the length of a string property without also needing to check for null.
For example, in the following code, checking the length of Label.Text should never cause a NullReferenceException to be thrown:
void SetLabelTextIfNotEmpty(Label label)
{
if (label.Text.Length == 0)
{
label.Text = "User name:";
}
}
þ DO treat a null reference (Nothing in Visual Basic and nullptr in C++/CLI) and an empty string ("") as equivalent.
For example, the following method uses the default value name when passed either a null reference or an empty string.
public string GetValue(string name)
{
if (string.IsNullOrEmpty(name))
name = GetDefaultName();
// Retrieve the value
[...]
}