i used IList with my day to day linq quaries but curious to know in simple words what is the diffrence between them i mean List and IList ?
Loading
i used IList with my day to day linq quaries but curious to know in simple words what is the diffrence between them i mean List and IList ?
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.
Jayraj ChhayaPosted Feb 27, 2024, 7:43 AM
Hi Abhishek Yadav,
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.Sam HobbsPosted Feb 27, 2024, 5:20 PM
It is easy to understand the difference when we understand what interfaces are. An interface has methods defined but no implementation for the methods. It is not possible to declare (allocate using the
newoperator) an interface. Use of an interface requires defining a class that inherits the interface and that implements the methods. Or an interface can be used to specify that any such class can be used.Note that there are the following in the
System.Collections.Genericnamespace.IList Interface
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.ilist-1?view=net-8.0
List Class
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-8.0
And the following in the
System.Collectionsnamespace.IList Interface
https://learn.microsoft.com/en-us/dotnet/api/system.collections.ilist?view=net-8.0
ArrayList Class
https://learn.microsoft.com/en-us/dotnet/api/system.collections.arraylist?view=net-8.0
Abhishek YadavPosted Feb 27, 2024, 7:45 AM
Jayraj Chhaya beautifull explantion brother