0

I went over the code of a List in C#

https://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,cf7f4095e4de7646

why is the list implemented as an array and not as a linked list?

I know memory is an issue when using for example double linked list comparing to an array, however when you remove/add a node to a list you will have less of a performance impact.

Can you please explain? Thanks

Gilad
  • 6,437
  • 14
  • 61
  • 119
  • 2
    How would you access linked list by index? – user4003407 Jul 07 '18 at 09:03
  • Among other reasons: It was meant to (generically) replace ArrayList. – TaW Jul 07 '18 at 09:10
  • I want to add and remove items, that is the goal of list without a limitation of space, i.e I don't have to resize all of the time, like in arrays. yes accessing to data is important but also removing and adding, I want to iterate of the list, not only access by index. – Gilad Jul 07 '18 at 09:12
  • @OrelEraki thanks I wan't able to find it. – Gilad Jul 07 '18 at 09:13
  • 1
    Look especially at Hans's remarks; they actually do answer the question.. – TaW Jul 07 '18 at 09:18

1 Answers1

4

First of all, there is a LinkedList in C#.

As for differences between array and linked list, an array provides fast and random access to the element in collection while in linked list, elements can’t be accessed randomly but can be accessed only sequentially and accessing element takes 0(n) time. Though there are some advantages of using LinkedList instead. This article provides detailed comparison.

Alex Riabov
  • 8,655
  • 5
  • 47
  • 48