Add a range of items to the beginning of a list?

Dave New picture Dave New · Dec 5, 2012 · Viewed 11.1k times · Source

The method List<T>.AddRange(IEnumerable<T>) adds a collection of items to the end of the list:

myList.AddRange(moreItems); // Adds moreItems to the end of myList

What is the best way to add a collection of items (as some IEnumerable<T>) to the beginning of the list?

Answer

Sergey Berezovskiy picture Sergey Berezovskiy · Dec 5, 2012

Use InsertRange method:

 myList.InsertRange(0, moreItems);