can someone explain me the difference between IList and List ?
The text of this thread is not in this database — only its details are. Read it on the old site: can someone explain me the difference between IList and List ?
The text of this thread is not in this database — only its details are. Read it on the old site: can someone explain me the difference between IList and List ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Mar 14, 2024, 3:28 PM
The difference is easy to understand when you understand interfaces. C# has very many interfaces.
You cannot create (get memory for) an instance of an interface; in other words you cannot do either of the following:
C# (actually .Net) uses
IListwhen aclassorstructthat derives fromIListcan be used. In other words, as a beginner you normally cannot useIList. When .Net usesIListyou will usually useList<>.Tuhin PaulPosted Mar 14, 2024, 7:40 PM
For example, the List class in C# implements the IList interface. So, instead of directly instantiating an IList, you would typically use List:
List thelist = new List();
Tuhin PaulPosted Mar 14, 2024, 7:37 PM
Instead of IList, use a more relatable interface like IShape. Show how a class like Circle or Square can implement IShape and provide its specific implementation details.
You could mention other common interfaces like IComparable or IEnumerable and how they are used in collections.
Jayraj ChhayaPosted Mar 14, 2024, 7:19 AM
ListandIListare related but have distinct differences.Listis a class that implementsIList, which is an interface.Listprovides more functionality and is mutable, allowing you to add, remove, and modify elements. On the other hand,IListis an interface that defines properties and methods for collections.Where to Use:
Listwhen you need a dynamic array that can grow or shrink in size.IListwhen you want to work with collections in a more generic way without specifying a particular implementation.Real-Life Example:
Imagine you have a shopping cart. You would use a
Listto store the items because you need to add, remove, or update items dynamically. However, if you have a method that accepts any collection type, you would useIListas a parameter type.In summary,
Listis mutable and provides more features, whileIListis more generic and allows flexibility in working with different collection types.Abhishek YadavPosted Mar 14, 2024, 5:54 AM
In C#, the `List` class is a generic class that is part of the `System.Collections.Generic` namespace, while the `IList` interface is a non-generic interface that is part of the `System.Collections` namespace.
Here are some key differences between `List` and `IList` in C#:
1. Type safety: `List` is a strongly typed generic collection class, which means that it can only store elements of a specific type. On the other hand, `IList` is not type-safe and can store elements of any type.
2. Methods: `List` class includes additional methods, such as `AddRange`, `InsertRange`, `RemoveRange`, `Sort`, etc., which are not available in the `IList` interface.
3. Performance: `List` is generally more performant than `IList` because it is a concrete class, while `IList` is an interface that can be implemented in different ways.
4. Inheritance: `List` is a concrete class, while `IList` is an interface. This means that a class can inherit from `List`, but it can only implement `IList`.
Overall, `List` is the preferred choice for most scenarios due to its type safety, additional methods, and better performance compared to `IList`.