공통 액세스 방법 집합 - Enumerable

17426 단어 enum
이것은 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;

        }

    }

}

좋은 웹페이지 즐겨찾기