List< T> .FindAll的结果是否保证与原始列表的顺序相同? [英] Is the result of List<T>.FindAll guaranteed to be in the same order as the original list?

查看:51
本文介绍了List< T> .FindAll的结果是否保证与原始列表的顺序相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个包含以下条目的列表:

If I've got a List with the following entries:

苹果 香蕉 葡萄 樱桃 橘子 猕猴桃

Apple Banana Grape Cherry Orange Kiwi

fruit.FindAll(f => f.Length == 6)

保证始终为

香蕉 樱桃 橙色

还是顺序可以不同?

推荐答案

从文档中未说明的意义上不能保证,但是如果您查看当前的实现方式,那么是的,它总是会以相同的顺序返回.

It's not guaranteed in the sense that it doesn't say it in the documentation, however if you look at how it's currently implemented, then yes, it'll always come back in the same order.

这是当前 实施的方式:

public List<T> FindAll(Predicate<T> match)
{
    if (match == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
    }
    List<T> list = new List<T>();
    for (int i = 0; i < this._size; i++)
    {
        if (match(this._items[i]))
        {
            list.Add(this._items[i]);
        }
    }
    return list;
}

如您所见,这是一个简单的for循环,它顺序地遍历列表,并添加匹配的项.

As you can see, it's a simple for loop that sequentially goes through the list, and adds the items that match.

这篇关于List&lt; T&gt; .FindAll的结果是否保证与原始列表的顺序相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆