【问题标题】:Is there a constraint, interface or class that envelops IEnumerable or array?是否存在包含 IEnumerable 或数组的约束、接口或类?
【发布时间】:2009-08-11 21:14:27
【问题描述】:

如果可能,我想将以下内容合并:

public static T[] ForEach<T>(this T[] TArray, Action<T> doWhat)
    {
        foreach (var item in TArray)
        {
            doWhat(item);
        }
        return TArray;
    }

上面的句柄数组,下面的句柄列表

    public static IEnumerable<T> ForEach<T>(this IEnumerable<T> TList, Action<T> action)
    {
        foreach (var item in TList)
            action(item);
        return TList;
    }

有什么共同点既可以继承自接口,也可以作为接口实现?

【问题讨论】:

    标签: arrays .net-3.5 list


    【解决方案1】:

    数组继承自 IEnumerable。

    【讨论】:

    • 换句话说:您已经可以将数组传递给函数的第二个版本。
    • 我错过了,因为在我编写数组接受函数之前,我忘记为这部分代码导入我的命名空间。
    猜你喜欢
    • 2022-06-11
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 2022-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-16
    相关资源
    最近更新 更多