Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is List<T> really an undercover Array in C#?

Tags:

c#

.net

I have been looking at .NET libraries using ILSpy and have come across List<T> class definition in System.Collections.Generic namespace. I see that the class uses methods like this one:

// System.Collections.Generic.List<T> /// <summary>Removes all elements from the <see cref="T:System.Collections.Generic.List`1" />.</summary> public void Clear() {     if (this._size > 0)     {         Array.Clear(this._items, 0, this._size);         this._size = 0;     }     this._version++; } 

So, the Clear() method of the List<T> class actually uses Array.Clear method. I have seen many other List<T> methods that use Array stuff in the body.

Does this mean that List<T> is actually an undercover Array or List only uses some part of Array methods?

I know lists are type safe and don't require boxing/unboxing but this has confused me a bit.

like image 638
Antun Tun Avatar asked May 05 '13 17:05

Antun Tun


People also ask

Is list the same as array in C?

An array stores a fixed-size sequential collection of elements of the same type, whereas list is a generic collection.

Is list more efficient than array?

Arrays can store data very compactly and are more efficient for storing large amounts of data. Arrays are great for numerical operations; lists cannot directly handle math operations. For example, you can divide each element of an array by the same number with just one line of code.

Is list slower than array?

Slower Search Time:Linked list have slower search times than arrays as random access is not allowed. Unlike arrays where the elements can be search by index, linked list require iteration.

Is a list faster than an array?

An array is faster than a list in python since all the elements stored in an array are homogeneous i.e., they have the same data type whereas a list contains heterogeneous elements. Moreover, Python arrays are implemented in C which makes it a lot faster than lists that are built-in in Python itself.


2 Answers

The list class is not itself an array. In other words, it does not derive from an array. Instead it encapsulates an array that is used by the implementation to hold the list's member elements.

Since List<T> offers random access to its elements, and those elements are indexed 0..Count-1, using an array to store the elements is the obvious implementation.

like image 144
David Heffernan Avatar answered Sep 28 '22 15:09

David Heffernan


This tends to surprise C++ programmers that know std::list. A linked list, covered in .NET as well with the LinkedList class. And has the same perf characteristics, O(1) for inserts and deletes.

You should however in general avoid it. Linked lists do not perform well on modern processors. Which greatly depend on the cpu caches to get reasonable performance with memory that's many times slower than the execution core. A simple array is by far the data structure that takes most advantage of the cache. Accessing an element gives very high odds that subsequent elements are present in the cache as well. That is not the case for a linked list, elements tend to be scattered throughout the address space, make a cache miss likely. They can be very expensive, as much as 200 cycles with the cpu doing nothing but waiting on the memory sub-system to supply the data.

But do keep the perf characteristics in mind, adding or removing an element that is not at the end of the List costs O(n), just like an array. And a large List can generate a lot of garbage as the array needs to be expanded, setting the Capacity property up front can help a lot to avoid that. More about that in this answer. And otherwise the exact same concerns for std::vector<>.

like image 35
Hans Passant Avatar answered Sep 28 '22 13:09

Hans Passant