Common auxiliary method set-Enumerable

Source: Internet
Author: User

Here are some common auxiliary methods for the Enumerable type. For other types, see the Directory: Use the C #3.0 Extensions method-and common auxiliary method set.

The code is listed directly here.

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;        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.