.IsNullOrEmpty() for List and Dictionary

string.IsNullOrEmpty() in C# for strings is awesome.  I pass in a string, it tells me if it was null or blank.  Pre-trim it with something like this :

string.IsNullOrEmpty( ( var ?? "" ).Trim() )

and I know if I’m toast or not before the null reference exception or blank screen.

Well, what if I have a List<T>?  Or a Dictionary<T,U>?  Here’s extension methods I wrote for checking blank-ness:

public static bool IsNullOrEmpty( this IList List ) {
    return ( List == null || List.Count < 1 );
}

public static bool IsNullOrEmpty( this IDictionary Dictionary ) {
    return ( Dictionary == null || Dictionary.Count < 1 );
}

The added benefit of this is I can say:

myDict.IsNullOrEmpty()

which is usually more like the thought I had when I started writing the code.  So I also add this method:

public static bool IsNullOrEmpty( this string String ) {
    return string.IsNullOrEmpty( ( String ?? “” ).Trim() );
}

so I can call it like this:

myString.IsNullOrEmpty()

but since I’m coalescing to empty string before trimming, I can just as easily say:

public static bool IsNullOrEmpty( this string String ) {
    return ( (String ?? “”).Trim() != “” );
}

And for good measure, here’s a similar JavaScript function I wrote to check for blank-ness:

function isNullOrEmpty(val) {
  var empty = true,
      name = null;

  if ( typeof(val) === 'undefined' || val === null ) {
    return true; // It's null or undefined
  }
  if ( typeof(val) === 'string' ) {
    return (val === “); // It's a string that may or may not be blank
  }
  if ( typeof(val) === 'object' ) {
    if (value.constructor === Array && val.length === 0) {
      return true; // It's an empty array
    }
    for ( name in val ) {
      if ( val.hasOwnProperty(name) ) {
          empty = false;
          break;
      }
    }
    return empty; // It's an object that has or doesn't have data in it
  }
  // It's not null or empty
  return false;
}

And that, as we say, is null … or empty.  :D