How can I add to a List's first position? [duplicate]
I just have a List<> and I would like to add an item to this list but at the first position. List.add() add the item at the last.. How can I do that?.. Thanks for help!
Solution 1:
List<T>.Insert(0, item);
Solution 2:
myList.Insert(0, item);
Solution 3:
Use List.Insert(0, ...)
. But are you sure a LinkedList
isn't a better fit? Each time you insert an item into an array at a position other than the array end, all existing items will have to be copied to make space for the new one.
Solution 4:
Use List<T>.Insert(0, item)
or a LinkedList<T>.AddFirst()
.