常见辅助方法集 -- Enumerable

这里是一些针对 Enumerable 类型的常见的辅助方法, 对于其他的类型, 请参考目录: 善用 C# 3.0 Extensions 方法 -- 以及常用辅助方法集

这里就直接把代码列出了。

public static class EnumerableExtension
{
    public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
    {
        Expect.ArgumentNotNull(action, "action");

        foreach (T t in enumerable)
        {
            action(t);
        }
    }

    public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T, int> action)
    {
        Expect.ArgumentNotNull(action, "action");

        int i = 0;
        foreach (T t in enumerable)
        {
            action(t, i++);
        }
    }

    /// <summary> Join the enumerable to a string.</summary>
    public static string Join<T>(this IEnumerable<T> enumerable, string start, string end, string seperator)
    {
        return Join<T>(enumerable, start, end, seperator, e => e);
    }

    /// <summary> Join the enumerable to a string.</summary>
    public static string Join<T>(this IEnumerable<T> enumerable, string start, string end, string seperator, Func<T, object> converter)
    {
        Expect.ArgumentNotNull(converter, "converter");

        StringBuilder sb = new StringBuilder();
        sb.Append(start);

        enumerable.ForEach((e, i) => sb.AppendFormat("{0}{1}", i == 0 ? string.Empty : seperator, converter(e)));

        sb.Append(end);

        return sb.ToString();
    }

    /// <summary>
    /// Generate an enumerable by repeating the given delegate.
    /// </summary>
    public static IEnumerable<T> Repeat<T>(this Func<T> generateResultFunc, int count)
    {
        for (int i = 0; i < count; ++i)
        {
            yield return generateResultFunc();
        }
    }

    public static IDictionary<TKey, TValue> Clone<TKey, TValue>(this IDictionary<TKey, TValue> source)
    {
        Dictionary<TKey, TValue> dict = source as Dictionary<TKey, TValue>;
        if (dict != null)
        {
            return new Dictionary<TKey, TValue>(source, dict.Comparer);
        }

        SortedDictionary<TKey, TValue> sortedDict = source as SortedDictionary<TKey, TValue>;
        if (sortedDict != null)
        {
            return new SortedDictionary<TKey, TValue>(source, sortedDict.Comparer);
        }

        SortedList<TKey, TValue> sortedList = source as SortedList<TKey, TValue>;
        if (sortedList != null)
        {
            return new SortedList<TKey, TValue>(source, sortedList.Comparer);
        }

        return new Dictionary<TKey, TValue>(source);
    }

    public static IList<T> Clone<T>(this IList<T> source)
    {
        return new List<T>(source);
    }

    public static Stack<T> Clone<T>(this Stack<T> source)
    {
        return new Stack<T>(source.Reverse());
    }

    public static Queue<T> Clone<T>(this Queue<T> source)
    {
        return new Queue<T>(source);
    }

    /// <summary>Merge the two dictionary. </summary>
    public static void Merge<TKey, TValue>(this IDictionary<TKey, TValue> left, IDictionary<TKey, TValue> right)
    {
        Expect.ArgumentNotNull(left, "left");
        Expect.ArgumentNotNull(right, "right");

        foreach (var kvp in right)
        {
            left[kvp.Key] = kvp.Value;
        }
    }
}

posted @ 2009-12-29 14:38  Ahha  阅读(1308)  评论(0编辑  收藏  举报