Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you insert at position 0 in a List<MyObject>?

Tags:

c#

collections

I need to insert an object at the beginning of a collection.

My colleciton is of type List

How can I do this?

like image 439
mrblah Avatar asked Oct 18 '09 21:10

mrblah


People also ask

How do I add an item to an index 0 in Python?

Solution: Use the list. insert(0, x) element to insert the element x at the first position 0 in the list.

What does List insert Do C#?

The Insert method of List<T> class inserts an object at a given position. The first parameter of the method is the 0th based index in the List. <T>. The InsertRange() method can insert a collection at the given position.


1 Answers

Sure can; for example a generic List of strings:

List<string> values = new List<string>();
values.Insert(0, "NewString");
like image 186
Paul Mason Avatar answered Oct 04 '22 15:10

Paul Mason