вторник, 22 сентября 2009 г.

Любимые extension methods

Расширения для событий:

static void Fire<T>(this EventHandler<T> handler, object sender, T eventArgs) where T : System.EventArgs
{
     if (handler != null)
          handler(sender, eventArgs);
}
public static void Fire(this EventHandler handler, object sender, System.EventArgs eventArgs)
{
     if (handler != null)
          handler(sender, eventArgs);
}

Расширения для коллекций:

public static bool IsEmpty<T>(this ICollection<T> list)
{
     return list == null || list.Count == 0;
}
public static void Foreach<T>(this IEnumerable<T> collection, Action<T> action)
{
     if (collection != null)
          foreach (var item in collection)
               action(item);
}

Расширения для строк:

public static int ToInt(this string value)
{
     int i;
     int.TryParse(value, out i);
     return i;
}
public static bool IsEmpty(this string value)
{
     return string.IsNullOrEmpty(value);
}
public static bool PoorEquals(this string value, string value2)
{
     return value != null && value2 != null 
            && value.Equals(value2, 
                 StringComparison.InvariantCultureIgnoreCase);
}

Комментариев нет:

Отправить комментарий