Array vs List CSharp

Featured image

When traversing collections of data in C#, developers wield a diverse arsenal of tools. Among the most prominent stand Arrays and Lists, powerful contenders often vying for the same role. Yet, beneath their shared purpose lies a nuanced tapestry of advantages and drawbacks, beckoning careful evaluation before making the optimal choice.

Arrays

An Array is a built in type in c#, generally an array is the first way that an aspiring developer is introduced to collections.

The below creates an array of Integers

var array = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

Advantages

  • Arrays in c# are strongly typed and as such they will have better performance as boxing/unboxing will not occur.
  • Arrays will consume less memory.
  • As arrays are strongly typed any type mismatches will be caught at compile time.

Disadvantages

  • Arrays require a size set on initilisation and this cannot be changed.
  • Items are stored consecutively in memory it is costly to insert or delete an item in an array.

For more information on Arrays in c# Click Here

Lists

A List is a generic collection in c# that represents a strongly typed list of objects that can be accessed by index.

The below creates an List of Integers

var list = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

Advantages

  • Lists provide methods to search, sort and manipulate (Add / Remove items).
  • The size does not need to be determined at initilisation.

Disadvantages

  • Lists are more costly Initialise compared to arrays.
  • Lists consume more memory compared to arrays.
  • As Lists are generic this allows for type mismatch runtime errors.

For more information on the List generic class in c# Click Here

Benchmark

All following tests the creation of Arrays or Lists of identical objects

  • A set of 11 and a set of 101 Integers
  • A set of 11 and a set of 101 Strings
  • A set of 11 and a set of 101 Classes
  • A set of 11 Records
| Test            | Mean       | Error       | StdDev    | Gen0   | Gen1   | Allocated |
|---------------- |-----------:|------------:|----------:|-------:|-------:|----------:|
| IntegerArray11  |   3.153 ns |   0.4993 ns | 0.0274 ns | 0.0046 |      - |      72 B |
| IntegerList11   |  27.487 ns |   3.8722 ns | 0.2123 ns | 0.0138 |      - |     216 B |
| IntegerArray101 |  13.875 ns |  13.2419 ns | 0.7258 ns | 0.0250 |      - |     392 B |
| IntegerList101  | 117.153 ns |  54.3591 ns | 2.9796 ns | 0.0755 | 0.0001 |    1184 B |
| StringrArray11  |   4.410 ns |   1.0272 ns | 0.0563 ns | 0.0071 |      - |     112 B |
| StringList11    |  41.022 ns |   9.3459 ns | 0.5123 ns | 0.0209 |      - |     328 B |
| StringrArray101 |  33.412 ns |  10.9841 ns | 0.6021 ns | 0.0530 |      - |     832 B |
| StringList101   | 166.764 ns |  37.4811 ns | 2.0545 ns | 0.1397 | 0.0007 |    2192 B |
| ClassArray11    |  41.823 ns |   9.1801 ns | 0.5032 ns | 0.0352 | 0.0001 |     552 B |
| ClassList11     |  84.088 ns |  28.9425 ns | 1.5864 ns | 0.0489 |      - |     768 B |
| ClassArray101   | 373.902 ns |  41.7738 ns | 2.2898 ns | 0.3104 | 0.0052 |    4872 B |
| ClassList101    | 601.145 ns | 161.2419 ns | 8.8382 ns | 0.3967 | 0.0076 |    6232 B |
| RecordArray     |  42.003 ns |   9.5369 ns | 0.5228 ns | 0.0352 | 0.0001 |     552 B |
| RecordList      |  89.668 ns |  64.3446 ns | 3.5269 ns | 0.0489 |      - |     768 B |

In all tests the time that was required to construct an array as well as the amount of memory used was significantly less compared to using a List. The above shows that on average it took nine times longer to construct a List of integers and consumed three times the memory compared to an Array.

Conclusion

Arrays and Lists both have their uses in c#, if the intent is to store a set of data that will not change (i.e. a list of currencies that your app supports) then an Array is the better option, however if you require dynamism in your set then a List is better suited.