7

When trying to figure out if a string is null or empty, I usually have the string already. That's why I would have expected a utility function such as String.IsNullOrEmpty() to work without parameters:

String myString;
bool test=myString.IsNullOrEmpty();

However, this does not work, because IsNullOrEmpty expects a String parameter. Instead, I have to write:

String myString;
bool test=String.IsNullOrEmpty(myString);

Why is this so? It seems unnecessarily clunky. Of course I can easily write own extension method for this, but it seems like a very obvious omission, so I am wondering if there is any good reason for this. I can't believe that the parameterless overload of this function has just been forgotten by Microsoft.

Adrian Grigore
  • 33,034
  • 36
  • 130
  • 210

8 Answers8

28

If the String would be null, calling IsNullOrEmpty() would cause a NullReferenceException.

 String test = null;

 test.IsNullOrEmpty(); // Instance method causes NullReferenceException

Now we have extension methods and we can implement this with an extension method and avoid the exception. But allways keep in mind that this only works because extension methods are nothing more than syntactical sugar for static methods.

public static class StringExtension
{
   public static Boolean IsNullOrEmpty(this String text)
   {
      return String.IsNullOrEmpty(text);
   }
}

With this extension method the follwing will never thrown an exception

 String test = null;

 test.IsNullOrEmpty(); // Extension method causes no NullReferenceException

because it is just syntactical sugar for this.

 StringExtension.IsNullOrEmpty(test);
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
27

This method has been around long before extension methods were added to C#, and before extension methods, there was no way to define an instance method/property such as xyz.IsNullOrEmpty() that you could still call if xyz was null.

mqp
  • 70,359
  • 14
  • 95
  • 123
  • 1
    Good point, although it makes me wonder why they don't fill the gap now that it would be possible. – Adrian Grigore Apr 24 '09 at 16:52
  • 10
    IsNullOrEmpty seems like it would be most pleasantly implemented as a property, really, but unfortunately we don't have extension properties. – mqp Apr 24 '09 at 16:54
5

Before C# 3.0 and extension methods there was no way to call a method on a null object.

RossFabricant
  • 12,364
  • 3
  • 41
  • 50
3

The reason there is no instance method is because if the string is indeed null, myString.IsNullOrEmpty would throw.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
2

I've created an extension method -- IsNothing -- that will check not only if it is null or empty, but also only contains spaces.

public static bool IsNothing( this string source )
{
    if (source == null || source.Length == 0)
    {
        return true;
    }
    else if (source.Trim().Length == 0)
    {
        return true;
    }
    return false;
}
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
1

Whilst it is entirely possible (and valid) to define an extension method to achieve this, I would be concerned that it looks strange syntactically, in that it appears to be a method call on an object which could be null.

YMMV, but I will be sticking with String.IsNullOrEmpty.

Chris Ballard
  • 3,771
  • 4
  • 28
  • 40
0

You could make this happen in C# 3.0:

public static class StringExtensions
{
    public static bool IsNullOrEmpty(this string value)
    {
        return string.IsNullOrEmpty(value);
    }
}

Then, this will work:

test.IsNullOrEmpty();

BUT

It is important to know that many consider this to be bad practice. This is because it violates the expectations of the user. Use with that caution.

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
  • Could you elaborate a bit more why you would regard this as bad practice? I was suspecting something like this, which is why I asked the question in the first place, but I still can't see why intuitive behaviour of a class would be bad, nevermind how it is implemented. I don't see a way the extension method could become harmful, especially since it is only syntactic sugar anyway. – Adrian Grigore Apr 24 '09 at 17:06
0

If you are in the process of adding extension methods or helper methods for testing your strings you might want to also add one that trims the string as well as most of us in many cases also do not want a string that has whitespace such as a tab or spaces.

Here is one of our test methods

public static bool IsTrimStringNullOrEmpty(string value)
{
    if(value == null) return true;
    if(value.Trim() == string.Empty) return true;
    return false;
}

OR

If you want to test the value and have it trimmed at the same time if NotNullOrEmpty

public static bool IsTrimStringNullOrEmpty(ref string value)
{
    if(value == null) return true;
    value = value.Trim();
    if(value == string.Empty) return true;
    return false;
}

Jim Scott
  • 2,493
  • 1
  • 18
  • 16