C# 列表IList, 集合ICollection, 可迭代容器IEnumerable 扩展

csharp 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

using UnityEngine;

/// <summary>迭代器扩展方法</summary>
[DebuggerStepThrough]
public static class EnumerableExtension
{
    /// <summary> 遍历操作 </summary>
    public static void ForEach<T>(this IEnumerable<T> values, Action<T> action)
    {
        foreach (T ele in values) 
            action(ele);
    }
    /// <summary> 遍历操作 </summary>
    public static void ForEach(this IEnumerable values, Action<object> action)
    {
        foreach (object ele in values)
            action(ele);
    }
    /// <summary> 计数</summary>
    public static int CountOf<T>(this IEnumerable<T> values, Predicate<T> match)
    {
        int ret = 0;
        foreach (T ele in values)
            ret += match(ele) ? 1 : 0;
        return ret;
    }
    /// <summary> 计数</summary>
    public static int CountOf(this IEnumerable values, Predicate<object> match)
    {
        int ret = 0;
        foreach (object ele in values)
            ret += match(ele) ? 1 : 0;
        return ret;
    }

    /// <summary> 查找</summary>
    public static T Find<T>(this IEnumerable<T> values, Predicate<T> match)
    {
        foreach (var ele in values)
        {
            if (match(ele)) return ele;
        }
        return default;
    }
    /// <summary> 查找</summary>
    public static object Find(this IEnumerable values, Predicate<object> match)
    {
        foreach (object ele in values)
        {
            if (match(ele)) return ele;
        }
        return default;
    }

    /// <summary> 查找</summary>
    public static T Find<T>(this IEnumerable<T> values, int index)
    {
        foreach(var ele in values)
        {
            if (index-- == 0)
                return ele;
        }
        return default;
    }

    /// <summary> 查找下标</summary>
    public static int FindIndex<T>(this IEnumerable<T> values, T data)
    {
        int i = 0;
        foreach (var ele in values)
        {
            if (ele.Equals(data))
                return i;
            ++i;
        }
        return -1;
    }
    /// <summary> 查找下标</summary>
    public static int FindIndex<T>(this IEnumerable<T> values, Predicate<T> match)
    {
        int i = 0;
        foreach (T ele in values)
        {
            if (match(ele))
                return i;
            ++i;
        }
        return -1;
    }
    /// <summary> 查找下标</summary>
    public static int FindIndex(this IEnumerable values, Predicate<object> match)
    {
        int i = 0;
        foreach (object ele in values)
        {
            if (match(ele))
                return i;
            ++i;
        }
        return -1;
    }
    /// <summary>查找全部</summary>
    public static List<T> FindAll<T>(this IEnumerable<T> values, Predicate<T> match)
    {
        List<T> ret = new List<T>();
        foreach (T ele in values)
            if (match(ele))
                ret.Add(ele);
        return ret;
    }

    /// <summary>是否存在</summary>
    public static bool Exist<T>(this IEnumerable<T> values, Predicate<T> match)
    {
        return values.FindIndex(match) != -1;
    }

    /// <summary> 是否为空</summary>
    public static bool Empty(this IEnumerable values) => !values.GetEnumerator().MoveNext();

    /// <summary> 转化为目标数组</summary>
    public static List<object[]> ToObjectArr(this IEnumerable values)
    {
        List<object[]> ret = new List<object[]>(values is ICollection c ? c.Count : 4);
        foreach (object ele in values)
            ret.Add(new object[] { ele });
        return ret;
    }

    /// <summary> 转化为目标数组</summary>
    public static List<object[]> ToObjectArr<T, TOut>(this IEnumerable<T> values, Func<T, TOut> func)
    {
        List<object[]> ret = new List<object[]>(values is ICollection c ? c.Count : 4);
        foreach (T ele in values)
            ret.Add(new object[] { ele, func.Invoke(ele) });
        return ret;
    }
}
相关推荐
JieE21213 小时前
LeetCode 56. 合并区间|超清晰 JS 图解思路,面试高频区间题
javascript·算法·面试
Jack2021 小时前
HarmonyOS开发中错误处理策略:网络异常统一处理
算法
小小杨树1 天前
读懂色彩:拍照调色不再难
算法·计算机视觉·配色
JieE2122 天前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE2122 天前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
vivo互联网技术2 天前
CVPR 2026 | 全新强化学习框架 BeautyGRPO:重塑真实人像
算法·大模型·cvpr·影像
Darling噜啦啦2 天前
列表转树算法深度解析:从 Map 到 Reduce 的两种实现,面试高频考点
数据结构·算法·面试
Scout-leaf2 天前
C#摸鱼实录——IoC与DI案例详解
c#
咕白m6252 天前
使用 C# 在 Excel 中应用多种字体样式
后端·c#