When should I use a List vs a LinkedList

后端 未结 15 2264
挽巷
挽巷 2020-11-22 14:50

When is it better to use a List vs a LinkedList?

15条回答
  •  -上瘾入骨i
    2020-11-22 15:25

    In most cases, List is more useful. LinkedList will have less cost when adding/removing items in the middle of the list, whereas List can only cheaply add/remove at the end of the list.

    LinkedList is only at it's most efficient if you are accessing sequential data (either forwards or backwards) - random access is relatively expensive since it must walk the chain each time (hence why it doesn't have an indexer). However, because a List is essentially just an array (with a wrapper) random access is fine.

    List also offers a lot of support methods - Find, ToArray, etc; however, these are also available for LinkedList with .NET 3.5/C# 3.0 via extension methods - so that is less of a factor.

提交回复
热议问题