Add method Adds an object to the end of the List<T>
What would be a quick and efficient way of adding object to the beginning of a list
Add method Adds an object to the end of the List<T>
What would be a quick and efficient way of adding object to the beginning of a list
Well, list.Insert(0, obj)
- but that has to move everything. If you need to be able to insert at the start efficiently, consider a Stack<T>
or a LinkedList<T>
Redefine what you use as beginning and end of the list, so that the last element is the beginning of the list. Then you just use Add to put an element at the beginning of the list, which is much more efficient than inserting items at position zero.