Condition for a non-empty string

c#

Solution

If `str` is `null`, then `str != ""` will return `true`, but `str.Length > 0` will throw a `NullReferenceException`. Other than that, they are equivalent.

But there are also a few other methods might use, like `string.IsNullOrEmpty` or `string.IsNullOrWhiteSpace`.

Problem

I've been debating what seems like a small, trivial idea, but I am curious if there is more to it. Is there a difference between using the following two conditions to detect if a string is not empty? Are there any cases where this would return different results, or any subtleties that makes one a better choice? ``` str != "" ``` or ``` str.Length > 0 ```

Original source