tags:

views:

104

answers:

4

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

My colleciton is of type List

How can I do this?

+3  A: 

Sure can; for example a generic List of strings:

        List<string> values = new List<string>();
        values.Insert(0, "NewString");
Paul Mason
+7  A: 

You can use the Insert method.

List<string> strings = new List<string> { "B", "C", "D" };
strings.Insert(0, "A");

MSDN documentation: http://msdn.microsoft.com/en-us/library/sey5k5z4.aspx

Shannon Cornish
A: 

Take a look at the Insert() method

myList.Insert(0, [item to insert])
Russ Cam
A: 

Inserting an item at index 0 will place the inserted object at the beginning of the list and the other items will be shifted up by one.

nasufara