✅ 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
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.
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. The interface is just a contract of what methods it must provide, it doesnt force an implementation detail like using an array
No.
IList<T>
is an interface. You can implement it however you like in your own classOkay, 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 ?
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.
Ok. Thank a lot! It was really helpful
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
I was confused, cause interfaces in c#, as I know, can even have variables as properties
And when I noticed this property (see picture)
yes, properties can be defined in interfaces
(they're basically methods)
But going deeply I got that it is not an array
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
Roughly, I was trying to find property of array[T] somewhere inside
cause people said it is based on array
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
wow
i didn't know that
if you go here https://source.dot.net/#System.Private.CoreLib/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs,25 you'll see that List<T> does in fact have an array inside it
Thank you!
$close
Use the /close command to mark a forum thread as answered
You should have permission to do that yourself, as an Associate