You could be familiar with these two methods of string. And today we are going to dive into them to see what the difference between them is.
What is it?
IsNullOrEmpty
method indicates whether a specified is null or an empty string.IsNullOrWhiteSpace
method indicates whether a specified is null, empty, or consists of white-space characters.
How it works!
Let’s take a look at an example below.
static void Main(string[] args)
{
Console.WriteLine(string.IsNullOrWhiteSpace("\t"));
Console.WriteLine(string.IsNullOrEmpty("\t"));
/* Output
True
False
*/
Console.WriteLine(string.IsNullOrWhiteSpace("\n"));
Console.WriteLine(string.IsNullOrEmpty("\n"));
/* Output
True
False
*/
Console.WriteLine(string.IsNullOrWhiteSpace(" "));
Console.WriteLine(string.IsNullOrEmpty(" "));
/* Output
True
False
*/
Console.WriteLine(string.IsNullOrWhiteSpace(""));
Console.WriteLine(string.IsNullOrEmpty(""));
/* Output
True
True
*/
}
As you have seen, they returned 2 different results except the last one. In order to clarify this, we are now going to look at its code in more detail.
public static bool IsNullOrEmpty(string value)
{
if (value != null)
return value.Length == 0;
return true;
}
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;
}
As you can see, IsNullOrEmpty
method only checks the specified string whether it will be a null value or its length will be zero. So that’s why this method has returned False of 3 cases above. Similarly, IsNullOrWhiteSpace method also makes sure our specified string whether it is null or empty. However, it has a little different from IsNullOrEmpty method is that white-space characters are defined by the Unicode standard. The IsNullOrWhiteSpace method interprets any character that returns a value of true when it is passed to the Char.IsWhiteSpace method as a white-space character. It is kind of useful when we have a method that converts from a string to a number as below. It’s because the condition !string.IsNullOrEmpty(strValue)
will return True even if strValue parameter is a white-space or character escape sequences (“\n”, “\t”, …). And we will get an exception once Convert.ToInt32
method is called. And IsNullOrWhiteSpace
will address this one.
static void Main(string[] args)
{
string strValue = "\n";
ConvertToInt(strValue);
}
public static int ConvertToInt(string strValue)
{
if (!string.IsNullOrEmpty(strValue))
{
// This will throw an exception: System.FormatException: 'Input string was not in a correct format.'
return Convert.ToInt32(strValue);
}
if (!string.IsNullOrWhiteSpace(strValue))
{
return Convert.ToInt32(strValue);
}
return 0;
}
Let’s see another example below. In order to remove all leading and trailing white-space characters from the current string, we will use Trim method. However, in the case below, we will get an exception since name
is null and IsNullOrWhiteSpace
works.
static void Main(string[] args)
{
string name = null;
GetItemByName(name);
}
public static Item GetItemByName(string name)
{
if (!string.IsNullOrEmpty(name.Trim()))
{
// This will throw an exception: System.NullReferenceException: 'Object reference not set to an instance of an object.'
return new Item();
}
if (!string.IsNullOrWhiteSpace(name))
{
return new Item();
}
return null;
}
Conclusion
I am showing you how these two methods work. IsNullOrWhiteSpace
looks better, however, it depends on our situation what we need to handle and choose which one is suited for us.