在C#中有没有任何简洁优雅的方法可以同时遍历两个列表

本文关键字:遍历 列表 两个 任何 有没有 简洁 方法 | 更新日期: 2023-09-27 17:58:16

所以我有一些代码可以用来做之类的事情

        List<ParameterInfo> theseParams = this.Action.GetParameters().OrderBy(p => p.Name).ToList(),
                            otherParams = other.Action.GetParameters().OrderBy(p => p.Name).ToList();
        if(theseParams.Count != otherParams.Count)
            return false;
        for(int i = 0; i < theseParams.Count; ++i)
        {
            ParameterInfo thisParam = theseParams[i],
                          otherParam = otherParams[i];
            if(thisParam.Name != otherParam.Name)
                return false;
        }
        return true;

我想知道是否有一种紧凑的方法可以一次迭代到列表?

在C#中有没有任何简洁优雅的方法可以同时遍历两个列表

当然只使用Enumerable.ZipEnumerable.All

return theseParams.Count == otherParams.Count
    && theseParams.Zip(otherParams, (t,o) => new {These = t, Other =o})
    .All(x => x.These.Name == x.Other.Name);