The method IsNullOrWhiteSpace covers IsNullOrEmpty , but it also returns true if the string contains only white space characters. So its always better to use IsNullOrWhiteSpace than IsNullOrEmpty ?
C# | IsNullOrWhiteSpace() Method In C#, IsNullOrWhiteSpace() is a string method. It is used to check whether the specified string is null or contains only white-space characters. A string will be null if it has not been assigned a value or has explicitly been assigned a value of null.
The C# IsNullOrEmpty() method is used to check whether the specified string is null or an Empty string. It returns a boolean value either true or false.
In this blog we shall see the difference between string and String. Well technically both of these are the same but with a slight difference. While “string” is a datatype, whereas “String” represents a class. “string” is an alias for System.
The best practice is selecting the most appropriate one.
.Net Framework 4.0 Beta 2 has a new IsNullOrWhiteSpace() method for strings which generalizes the IsNullOrEmpty() method to also include other white space besides empty string.
The term “white space” includes all characters that are not visible on screen. For example, space, line break, tab and empty string are white space characters*.
Reference : Here
For performance, IsNullOrWhiteSpace is not ideal but is good. The method calls will result in a small performance penalty. Further, the IsWhiteSpace method itself has some indirections that can be removed if you are not using Unicode data. As always, premature optimization may be evil, but it is also fun.
Reference : Here
Check the source code (Reference Source .NET Framework 4.6.2)
IsNullorEmpty
[Pure]
public static bool IsNullOrEmpty(String value) {
return (value == null || value.Length == 0);
}
IsNullOrWhiteSpace
[Pure]
public static bool IsNullOrWhiteSpace(String value) {
if (value == null) return true;
for(int i = 0; i < value.Length; i++) {
if(!Char.IsWhiteSpace(value[i])) return false;
}
return true;
}
Examples
string nullString = null;
string emptyString = "";
string whitespaceString = " ";
string nonEmptyString = "abc123";
bool result;
result = String.IsNullOrEmpty(nullString); // true
result = String.IsNullOrEmpty(emptyString); // true
result = String.IsNullOrEmpty(whitespaceString); // false
result = String.IsNullOrEmpty(nonEmptyString); // false
result = String.IsNullOrWhiteSpace(nullString); // true
result = String.IsNullOrWhiteSpace(emptyString); // true
result = String.IsNullOrWhiteSpace(whitespaceString); // true
result = String.IsNullOrWhiteSpace(nonEmptyString); // false
The differences in practice :
string testString = "";
Console.WriteLine(string.Format("IsNullOrEmpty : {0}", string.IsNullOrEmpty(testString)));
Console.WriteLine(string.Format("IsNullOrWhiteSpace : {0}", string.IsNullOrWhiteSpace(testString)));
Console.ReadKey();
Result :
IsNullOrEmpty : True
IsNullOrWhiteSpace : True
**************************************************************
string testString = " MDS ";
IsNullOrEmpty : False
IsNullOrWhiteSpace : False
**************************************************************
string testString = " ";
IsNullOrEmpty : False
IsNullOrWhiteSpace : True
**************************************************************
string testString = string.Empty;
IsNullOrEmpty : True
IsNullOrWhiteSpace : True
**************************************************************
string testString = null;
IsNullOrEmpty : True
IsNullOrWhiteSpace : True
They are different functions. You should decide for your situation what do you need.
I don't consider using any of them as a bad practice. Most of the time IsNullOrEmpty()
is enough. But you have the choice :)
Here is the actual implementation of both methods ( decompiled using dotPeek)
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool IsNullOrEmpty(string value)
{
if (value != null)
return value.Length == 0;
else
return true;
}
/// <summary>
/// Indicates whether a specified string is null, empty, or consists only of white-space characters.
/// </summary>
///
/// <returns>
/// true if the <paramref name="value"/> parameter is null or <see cref="F:System.String.Empty"/>, or if <paramref name="value"/> consists exclusively of white-space characters.
/// </returns>
/// <param name="value">The string to test.</param>
public static bool IsNullOrWhiteSpace(string value)
{
if (value == null)
return true;
for (int index = 0; index < value.Length; ++index)
{
if (!char.IsWhiteSpace(value[index]))
return false;
}
return true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With