Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

list

lambda

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

Apple Banana Grape Cherry Orange Kiwi

Is the result of

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

guaranteed to always be

Banana Cherry Orange

or could the order be different?

like image 494
PaulB Avatar asked Feb 24 '10 16:02

PaulB


1 Answers

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.

Here's how it's 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, it's a simple for loop that sequentially goes through the list, and adds the items that match.

like image 135
BFree Avatar answered Nov 04 '22 20:11

BFree