I need to insert an object at the beginning of a collection.
My colleciton is of type List
How can I do this?
I need to insert an object at the beginning of a collection.
My colleciton is of type List
How can I do this?
Sure can; for example a generic List of strings:
List<string> values = new List<string>();
values.Insert(0, "NewString");
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
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.