Is the result of List <T> .FindAll guaranteed in the same order as the original list?

If I have a List with the following data:

Apple Banana Grapes Cherry Orange Kiwi

Is the result

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

always guaranteed

Banana Cherry Orange

or may the order be different?

+5
source share
5 answers

This is not guaranteed in the sense that he does not talk about it in the documentation, however, if you look at how this is currently implemented, then yes, it will always return in the same order.

Here's how it is currently implemented:

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;
}

As you can see, this is a simple loop that sequentially goes through the list and adds elements that match.

+6

.

, , . , - , . .

+4

List <T> .FindAll, , , , .

, , .

+2

List<T>.FindAll . , . , , , - . . , , .

+1

MSDN , , , .

0

All Articles