Data List C # Structure Efficiency

At the moment, I use List<short>as a buffer to store things for some time, while the calculation is done for each value based on other values โ€‹โ€‹located further in the buffer. Then I realized that this was probably not very effective, because I was told that List<>this is a linked list, so every time I do whatever = myList[100];, the poor have to jump from all the other nodes first to get the value I want. I do not want to use a regular array, because I have loads Add()and Remove()carrying in other places in the code. So I need a class that inheritsIList<T>but uses a regular array data structure. Does anyone know a class in .net that works this way, so I don't need to write it myself? I tried using an ArrayList, but it is not typical!

+5
source share
4 answers

No, List<T>this is a common collection, not a linked list. If you need to add and remove functionality, then List<T>this is the default implementation for most users.

+1
source

List<T> . , , , , . : , Remove/insert , / - O (n). , , , .

docs.

List (T) ArrayList. IList (T), , .

+8

List<T> , . List<T> .

+2

In addition to tvanfosson's correct answer, if you never know how something works inside, just download the .NET Reflector and you can see exactly how it is implemented. In this case, drilling to the indexer List<T>shows us the following code:

public T this[int index]
{
    get
    {
        if (index >= this._size)
        {
            ThrowHelper.ThrowArgumentOutOfRangeException();
        }
        return this._items[index];
    }
    // ...

where you can see that it this._items[index]is an array of type type T.

+2
source

All Articles