C
C#5mo ago
Kuno

✅ Hello. Can somebody clarify one thing about List<T>

Is it true, that List<T> is a dynamic array, when LinkedList<T> is a double linked list ?
22 Replies
canton7
canton75mo ago
Yes. List<T> is a wrapper around an array, and it increases the size of the array as you add more items. LinkedList<T> is a doubly-linked list. This information is available at the start of the documentation for both types.
The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface by using an array whose size is dynamically increased as required.
and
Represents a doubly linked list.
Kuno
Kuno5mo ago
In other words in .NET any class implementing IList<T> is based on array ? And depending on taken structure it can be either static array or dynamic array (it can be defined in following property) ?
No description
Pobiega
Pobiega5mo ago
No. The interface is just a contract of what methods it must provide, it doesnt force an implementation detail like using an array
canton7
canton75mo ago
No. IList<T> is an interface. You can implement it however you like in your own class
Kuno
Kuno5mo ago
Okay, I got you. Since ArrayList according to .NET documentation uses dynamic array and List<T> is just equivalent. That means that List<T> is based on array too ?
canton7
canton75mo ago
I don't see why ArrayList is relevant? That's an old C# 1 type, before generics were added. It is not relevant any more. Look at the List<T> docs: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-8.0. Under "Remarks", you can see "For more information about this API, see Supplemental API remarks for List<T>." with a link to https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-collections-generic-list%7Bt%7D. There you'll find the text:
The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface by using an array whose size is dynamically increased as required.
Kuno
Kuno5mo ago
Ok. Thank a lot! It was really helpful
Jimmacle
Jimmacle5mo ago
to add to this, interfaces just provide a set of methods that a class needs to implement so anything that implements IList<T> just has to "work like a list," it doesn't say anything about how that's actually implemented
Kuno
Kuno5mo ago
I was confused, cause interfaces in c#, as I know, can even have variables as properties And when I noticed this property (see picture)
Kuno
Kuno5mo ago
No description
Jimmacle
Jimmacle5mo ago
yes, properties can be defined in interfaces (they're basically methods)
Kuno
Kuno5mo ago
But going deeply I got that it is not an array
Jimmacle
Jimmacle5mo ago
an IList<T> isn't anything other than a contract, it doesn't tell a class how to do something just the methods it needs to define you could implement a list as an array, some variant of a linked list, whatever you want
Kuno
Kuno5mo ago
Roughly, I was trying to find property of array[T] somewhere inside cause people said it is based on array
Jimmacle
Jimmacle5mo ago
List<T> is, IList<T> does not guarantee any particular implementation you won't find the private details of classes on that website, you'd have to go to https://source.dot.net
Kuno
Kuno5mo ago
wow i didn't know that
Kuno
Kuno5mo ago
No description
Kuno
Kuno5mo ago
Thank you!
Jimmacle
Jimmacle5mo ago
$close
MODiX
MODiX5mo ago
Use the /close command to mark a forum thread as answered
canton7
canton75mo ago
You should have permission to do that yourself, as an Associate