Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't LinkedList(T) implement the IList(T) interface?

Tags:

c#

.net

In C#, the LinkedList(T) class does not implement the IList(T) interface. However, the List(T) class does implement IList(T). Why is there this discrepancy? Functionally, they are both lists, so this design choice seems odd. It now becomes difficult to switch implementations from List(T) to LinkedList(T).

like image 523
Michael Venable Avatar asked Aug 27 '10 13:08

Michael Venable


People also ask

Does List implement IList?

IList is an interface. List is one concrete type that implements the IList interface.

Which of the following is not implemented using List T interface?

1 Answer. To explain I would say: SessionList is not an implementation of List interface. The others are concrete classes of List.

Should I use List or IList?

Basically, if you want to create your own custom List , say a list class called BookList , then you can use the Interface to give you basic methods and structure to your new class. IList is for when you want to create your own, special sub-class that implements List.

What is the difference between List and IList in C#?

The main difference between List and IList in C# is that List is a class that represents a list of objects which can be accessed by index while IList is an interface that represents a collection of objects which can be accessed by index.


2 Answers

IList<T> interface contains an indexer, the indexer is not a functionality you expect on a LinkedList.

List<T> can assure access to items in O(1), LinkedList by definition of it's it structure can't provide access to items in O(1).

like image 52
Elisha Avatar answered Oct 02 '22 14:10

Elisha


See the definition of a linked list, and you will understand.

Main issue, LinkedLists can contain circular references, and thus does not have an index.

Linked lists are among the simplest and most common data structures; they provide an easy implementation for several important abstract data structures, including stacks, queues, associative arrays, and symbolic expressions.

The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk. For that reason, linked lists allow insertion and removal of nodes at any point in the list, with a constant number of operations.

On the other hand, linked lists by themselves do not allow random access to the data, or any form of efficient indexing. Thus, many basic operations — such as obtaining the last node of the list, or finding a node that contains a given datum, or locating the place where a new node should be inserted — may require scanning most of the list elements.

like image 39
Arcturus Avatar answered Oct 02 '22 15:10

Arcturus