Reputation: 237
Is there any point of using those data types other then legacy code? Other data types like Dictionary or Graph are understandably used because they provide extra / different functionality. But array, LinkedList or ArrayList have less of a functionality and sometimes worst performance then List (ArrayList is less memory efficient in value types)
Then why use them at all?
Note: this is not an opinion - based question. All I want to know is use cases for these types
Another Note: I know about Linked list's O(1) insert time. I am asking when should it be utilized over the standard List, which has O(1) access time? When it is better to use? (and the question about ArrayList and array remains)
Upvotes: 1
Views: 404
Reputation: 26352
Here is a list of differentiators from the List
implementation. You use it when the items in the list need to maintain a specific order (hence the next and previous references).
Represents a doubly linked list.
LinkedList<T>
provides separate nodes of typeLinkedListNode<T>
, so insert and removal are O(1) operations.
You can remove nodes and reinsert them, either in the same list or in another list, which results in no additional objects allocated on the heap. Because the list also maintains an internal count, getting the Count property is an O(1) operation.
Each node in a
LinkedList<T>
object is of the typeLinkedListNode<T>
. Because theLinkedList<T>
is doubly linked, each node points forward to the Next node and backward to the Previous node.
ArrayList
is a deprecated implementation used in the past. Prefer List<T>
generic implementation in any new code.
As a generic collection,
List<T>
implements the genericIEnumerable<T>
interface and can be used easily in LINQ
ArrayList
belongs to the days that C# didn't have generics. It's deprecated in favor ofList<T>
. You shouldn't useArrayList
in new code that targets .NET >= 2.0 unless you have to interface with an old API that uses it.
Array is a fixed size collection and it supports multiple dimensions. It is the most efficient of the three for simple insert and iterations.
Upvotes: 0
Reputation: 1062640
ArrayList
? sure: don't use it, basically ever (unless you don't want to migrate some legacy code, or can't because somebody has unwisely used BinaryFormatter
).
LinkedList<T>
, however, is not in the same category - it is niche, but it has uses due to cheap insertion/removal/etc, unlike List<T>
which would need to move data around to perform insertion/removal. In most scenarios, you probably don't need that feature, so: don't use it unless you do?
Upvotes: 2