I want to remove the first line:
!string.IsNullOrEmpty(cell.Text)
will this cause any issue?
I ran across this in some code:
if ((id % 2 == 0)
&& !string.IsNullOrEmpty(cell.Text)
&& !string.IsNullOrEmpty(cell.Text.Trim())
)
I think the first string.IsNullOrEmpty would return false on a string with spaces
and the line with Trim() takes care of that, so the first IsNullOrEmpty is useless
But before I remove the line without the trim I thought I'd run it by the group.
if cell.Text is null, you'd have an exception without that first check.
In .NET 4.0:
if (id % 2 == 0 && !string.IsNullOrWhiteSpace(cell.Text))
{
...
}
In older versions you should keep the two tests because if you remove the first and cell.Text
is null, you will get a NRE on the second when you try to invoke .Trim
on a null instance.
Or you could also do this:
if (id % 2 == 0 && string.IsNullOrWhiteSpace((cell.Text ?? string.Empty).Trim()))
{
...
}
or even better, you could write an extension method for the string type that will do this so that you could simply:
if (id % 2 == 0 && !cell.Text.IsNullOrWhiteSpace())
{
...
}
which might look like this:
public static class StringExtensions
{
public static bool IsNullOrWhiteSpace(this string value)
{
return string.IsNullOrEmpty((value ?? string.Empty).Trim());
}
}
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