Question

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.

Was it helpful?

Solution

The first IsNullOrEmpty catches null values before they throw a NullReferenceException with Trim().

However, there is a better way:

if ((id % 2 == 0) && !string.IsNullOrWhiteSpace(cell.Text))

OTHER TIPS

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());
    }
}

I believe the test is to ensure that cell.text is not null first... if so, trying to bypass it and get just cell.text.trim() would choke as you can't do a trim on a null string.

Why not use !string.IsNullOrWhitespace(call.Text) and drop the previous two checks?

You cannot remove just the first IsNullOrEmpty as the cell.Text could be null and thus calling Trim on it would throw and exception. Use IsNullOrWhiteSpace if you are using .Net 4.0 or leave both checks.

if ((id % 2 == 0) && !string.IsNullOrWhiteSpace(cell.Text))

If cell.Text is null, the expression string.IsNullOrEmpty(cell.Text.Trim()) will throw an exception since it is trying to run method Trim() on cell.

Much more readble if condition would be: cell.Text!=null && cell.Text.Trim()!=""

You can use extension method like this:

/// <summary>
/// Indicates whether the specified string is null or empty.
/// This methods internally uses string.IsNullOrEmpty by trimming the string first which string.IsNullOrEmpty doesn't.
/// .NET's default string.IsNullOrEmpty method return false if a string is just having one blank space.
/// For such cases this custom IsNullOrEmptyWithTrim method is useful.
/// </summary>
/// <returns><c>true</c> if the string is null or empty or just having blank spaces;<c>false</c> otherwise.</returns> 
public static bool IsNullOrEmptyWithTrim(this string value)
{
    bool isEmpty = string.IsNullOrEmpty(value);
    if (isEmpty)
    {
        return true;
    }
    return value.Trim().Length == 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top