9. Use String.Empty instead of ""
Good:
If ( name == String.Empty )
{
// do something
}
Not Good:
If ( name == "" )
{
// do something
}
I think you need the answer to two questions.
1) Should MS had created the String.Empty constant?
Answer: no.
It is best practice to replace magic literal values in code
with named constants.
For two reasons:
* it ensures a consistent change when the values has to be changed
* the constant name can provide more information about what the values
is than then just the value
But neither applies here:
* the value of String.Empty will never change from ""
* the name String.Empty is not providing more information
than ""
Purely aesthetic reasons are not sufficient to warrant creation
of a constant.
So please - never create a class:
public class FooBar
{
private const int ZERO = 0;
private const int ONE = 1;
...
}
But to some extent that does not really matter.
MS did create String.Empty and C# developers has started
using it.
So new question:
2) Since MS has created String.Empty should we use it?
Answer: it does not matter, but the same convention should
be used throughout the entire solution.
All developers will understand both String.Empty and "".
But the code is easier to read if a consistent style is used.
So decide on a style and stick to it.
Arne