In my opinion, it Listis mainly implemented using LinkedList, while the usual Arrayis implemented as continuous blocks. I always used Listbecause it is in the namespace Genericand because I thought that it uses dynamic memory allocation, but I was wrong.
Yesterday I saw an implementation Listusing Reflector and found that it is actually an array of T ( T[]). While controlling each element in Listthere is a lot Array.Copy. For example, when you use it Insert, it will create new memory and copy all the elements before / after the inserted elements. Therefore, it seems to me that the use is Listvery expensive.
I have seen SortedList. I'm not sure why it SortedListalso implements an array inside it. Don't you think SortedListit would be terrible to use an array, since you need to sort the list every time a little manipulation occurs List?
I also wonder why it is Listso popular that most people use it, not for LinkedList. Is it just because of the flexibility of the indexer?
source
share