why string.IsNullOrEmpty() is created?
If string.Empty != null
why string.IsNullOrEmpty()
is created?
I just want to sa开发者_如何学JAVAy that:
ifnull
and string.Empty
are different to each other.
- why
string.IsNull();
andstring.IsEmpty();
separate methods does not exist. - why a combined method
string.IsNullOrEmpty()
exists?
string.IsNull
doesn't exist because you'd just check for the reference being nullstring.IsEmpty
doesn't exist because you can easily compare for equality with "" or for a length of 0string.IsNullOrEmpty
exists because it's simpler to write the single method call than useif (text == null || text.Length == 0)
(or the inverse, of course).
Each of the individual checks can be done simply on its own, but it's convenient to have a combination of the two.
It's for checking that the input string is a valid one. (e.g, not null and not empty).
So you don't want to do both the checks each time you want to ensure that so that's why it is made for.
If you want to check either of the single ones you can just use the == null
or == ""
compares.
精彩评论