WPF_常用集合扩展方法

    public static class EnumerableExtensions
    {
        public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> toAction)
        {
            foreach (T each in enumerable)
            {
                toAction(each);
            }
        }

        public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T, int> toAction)
        {
            int i = 0;
            foreach (T each in enumerable)
            {
                toAction(each, i++);
            }
        }

        public static void ForEach<T>(this T[] enumerable, Action<T> toAction)
        {
            foreach (T each in enumerable)
            {
                toAction(each);
            }
        }

        public static void ForEach<T>(this T[] enumerable, Action<T, int> toAction)
        {
            int i = 0;
            foreach (T each in enumerable)
            {
                toAction(each, i++);
            }
        }


        /// <summary>
        /// 判断<paramref name="enumerable" />集合是否只有一个元素。
        /// </summary>
        public static bool IsSingle<T>(this IEnumerable<T> enumerable)
        {
            return enumerable.Any() && !enumerable.Skip(1).Any();
        }

        /// <summary>
        /// 判断<paramref name="enumerable" />集合是否只有一个符合<paramref name="predicate" />条件的元素。
        /// </summary>
        public static bool IsSingle<T>(this IEnumerable<T> enumerable, Func<T, bool> predicate)
        {
            int count = 0;
            foreach (T each in enumerable)
            {
                if (predicate(each))
                {
                    count++;
                    if (count == 2)
                    {
                        break;
                    }
                }
            }

            return count == 1;
        }

        /// <summary>
        /// 判断集合中的元素是否是同一种类型。
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="source">对象</param>
        /// <returns>只有一种元素<see langword="true"/></returns>
        public static bool IsSingleKind<T>(this IEnumerable<T> source)
        {
            using (var enumerator = source.GetEnumerator())
            {
                if (enumerator.MoveNext() == false)
                {
                    return false;
                }

                T first = enumerator.Current;
                while (enumerator.MoveNext())
                {
                    var current = enumerator.Current;
                    if (first == null)
                    {
                        if (current != null)
                        {
                            return false;
                        }
                    }
                    else if (first.Equals(current) == false)
                    {
                        return false;
                    }
                }
            }

            return true;
        }


        /// <summary>
        /// 返回集合中的最大值,如果没有就返回默认值<paramref name="defaultValue"/>
        /// </summary>
        public static T MaxOrDefault<T>(this IEnumerable<T> enumerable, T defaultValue = default(T))
        {
            return enumerable.Any() ? enumerable.Max() : defaultValue;
        }

        /// <summary>
        /// 返回集合中符合指定条件<paramref name="selector"/>的最大值,如果没有就返回默认值<paramref name="defaultValue"/>
        /// </summary>
        public static TResult MaxOrDefault<TSource, TResult>(this IEnumerable<TSource> enumerable,
                                                             Func<TSource, TResult> selector,
                                                             TResult defaultValue = default(TResult))
        {
            return enumerable.Any() ? enumerable.Max(selector) : defaultValue;
        }

        /// <summary>
        /// 返回集合中的最小值,如果没有就返回默认值<paramref name="defaultValue"/>
        /// </summary>
        public static T MinOrDefault<T>(this IEnumerable<T> enumerable, T defaultValue = default(T))
        {
            return enumerable.Any() ? enumerable.Min() : defaultValue;
        }

        /// <summary>
        /// 返回集合中符合指定条件<paramref name="selector"/>的最小值,如果没有就返回默认值<paramref name="defaultValue"/>
        /// </summary>
        public static TResult MinOrDefault<TSource, TResult>(this IEnumerable<TSource> enumerable,
                                                             Func<TSource, TResult> selector,
                                                             TResult defaultValue = default(TResult))
        {
            return enumerable.Any() ? enumerable.Min(selector) : defaultValue;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值