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;
    }
}
相关推荐
CS创新实验室19 小时前
算法、齿轮与硅基大脑:数值计算发展简史
人工智能·算法·数值计算
海石20 小时前
1563分的简单题,可能就简单在能被暴力AC
算法·leetcode
海石21 小时前
1400分的dp汗流浃背之【交替子数组计数】
算法·leetcode
奋发向前wcx21 小时前
P2590 树的统计 题目解析
数据结构·算法·深度优先
imbackneverdie1 天前
AI4S不止于分子药物:以MedPeer为代表的科研基建打开产业新增量
大数据·人工智能·算法·aigc·科研·学术·ai 4s
逝水无殇1 天前
C# 运算符重载详解
开发语言·后端·c#
额鹅恶饿呃1 天前
C语言中的数据结构和变量
c语言·数据结构·算法
运行时记录1 天前
prompt-optimizer skill
算法
万法若空1 天前
【数据结构-哈希表】哈希表原理
数据结构·算法·散列表
退休倒计时1 天前
【每日一题】LeetCode 437. 路径总和 III TypeScript
算法·leetcode·typescript