string.IsNullOrEmpty using Trim()

Brad Boyce picture Brad Boyce · Mar 14, 2012 · Viewed 8.7k times · Source

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.

Answer

jcomeau_ictx picture jcomeau_ictx · Mar 14, 2012

if cell.Text is null, you'd have an exception without that first check.