Feedback

C# - IsNull and IsNullOrEmpty Extension-Methods become Lazy

Veröffentlicht von am 11/17/2009
(1 Bewertungen)
Ich finde das macht den Code einfach besser lesbar und das Programmieren komfortabler.

Beispiel:

object abc = null;
string xyz = string.Empty;

if (abc.IsNull())
xyz = "abc is null!";

if (!xyz.IsNullOrEmpty())
abc = new object();


Update 11.01.22: IsNullOrEmpty becomes lazy.

PS: I love extension methods!

Gruß,
Chriss
namespace CKing.Extensions
{
  using System.Collections.Generic;
  using System.Linq;

  /// <summary>
  /// Contains basic extension-methods, like 'IsNull', 'IsNullOrEmpty', aso.
  /// </summary>
  public static class BasicExt
  {
    /// <summary>
    /// Determines whether the object is null.
    /// </summary>
    /// <param name="source">The object, which may be null.</param>
    /// <returns>
    ///     <c>true</c> if the object is null; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsNull(this object source)
    {
      return source == null;
    }

    /// <summary>
    /// Determines whether the string is null or contains no chars.
    /// </summary>
    /// <param name="source">The string, which may be null or empty.</param>
    /// <returns>
    ///     <c>true</c> if the string is null or empty; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsNullOrEmpty(this string source)
    {
      return string.IsNullOrEmpty(source);
    }

    /// <summary>
    /// Determines whether the collection is null or contains no elements.
    /// </summary>
    /// <typeparam name="T">The IEnumerable type.</typeparam>
    /// <param name="source">The enumerable, which may be null or empty.</param>
    /// <returns>
    ///     <c>true</c> if the IEnumerable is null or empty; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
    {
      if (source == null)
        return true;

      var sourceAsCollection = source as ICollection<T>;
      if (sourceAsCollection != null)
        return sourceAsCollection.Count < 1;

      return sourceAsCollection.Any();
    }

    /// <summary>
    /// Determines whether the collection is null or contains no elements.
    /// </summary>
    /// <typeparam name="T">The IEnumerable type.</typeparam>
    /// <param name="source">The collection, which may be null or empty.</param>
    /// <returns>
    ///     <c>true</c> if the IEnumerable is null or empty; otherwise, <c>false</c>.
    /// </returns>
    public static bool IsNullOrEmpty<T>(this ICollection<T> source)
    {
      if (source == null)
        return true;

      return source.Count < 1;
    }
  }
}

2 Kommentare zum Snippet

Christopher Kk schrieb am 11/17/2009:
Finde ich gut, aber IsNullOrEmpty wird von string doch schon unterstützt? Zwar auf Klassenebene und nicht auf Objektebene, aber da sehe ich jetzt keine Vorteile gegenüber der string-Implementierung.
Christian Köwing schrieb am 11/17/2009:
Oh gut, wusste ich gar nicht. See refresh.
 

Logge dich ein, um hier zu kommentieren!