Difference between List and IList

C#.Net

C# Problem Overview


> Possible Duplicates:
> IList<int> vs List<int>
> C# - List<T> or IList<T>

What is the difference between List and IList, which one has better performance and when to use List over IList and vice versa?

C# Solutions


Solution 1 - C#

In C# List is a concrete implementation of the IList interface. The List is an implementation of the IList Interface. The idea is to program against the interface, not the implementation. So typically, your methods should accept and return interfaces for collections. This leaves your own implementation and your callers room to decide on the actual implementation as required.

Benefit of using an Interface is that you get to implement your functionality or better yet, the only functionality you require. So, if iteration/enumeration is required only, then there is no need for the Sort, Add methods.

Solution 2 - C#

List implements IList interface

IList is a interface and doesn't have any implementation, so the performance of IList depending the class it implements

Solution 3 - C#

IList is the interface - see this question for more information - https://stackoverflow.com/questions/400135/c-listt-or-ilistt

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaxymusView Question on Stackoverflow
Solution 1 - C#Hassan GulzarView Answer on Stackoverflow
Solution 2 - C#Arsen MkrtchyanView Answer on Stackoverflow
Solution 3 - C#StuartView Answer on Stackoverflow