65.9K
CodeProject is changing. Read more.
Home

Handy Extension Methods

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.83/5 (3 votes)

May 30, 2011

CPOL
viewsIcon

29774

Here are a couple of my favorite extension methhods. What are yours?

using System.Collections.Generic;
using System.Text.RegularExpressions;

public static class ExtensionMethods
{
    /// <summary>
    /// Add a sequence to a collection.
    /// </summary>
    /// <typeparam name="T">Object type.</typeparam>
    /// <param name="coll">Generic collection.</param>
    /// <param name="seq">Generic sequence.</param>
    public static void Add<T>(this ICollection<T> coll, IEnumerable<T> seq)
    {
        foreach (T item in seq)
            coll.Add(item);
    }

    /// <summary>
    /// Indicates whether the specified string is null, empty, or whitespace.
    /// </summary>
    /// <param name="value">The string to test.</param>
    /// <returns>Value indicating whether string contains squat.</returns>
    public static bool IsNullEmptyOrWhitespace(this string value)
    {
        return string.IsNullOrEmpty(value) || !Regex.IsMatch(value, @"\S");
    }
}