Can you insert at position 0 in a List?

后端 未结 4 1981
醉话见心
醉话见心 2021-01-18 05:07

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

My colleciton is of type List

How can I do this?

相关标签:
4条回答
  • 2021-01-18 05:47

    Sure can; for example a generic List of strings:

    List<string> values = new List<string>();
    values.Insert(0, "NewString");
    
    0 讨论(0)
  • 2021-01-18 05:47

    Take a look at the Insert() method

    myList.Insert(0, [item to insert])
    
    0 讨论(0)
  • 2021-01-18 05:50

    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.

    0 讨论(0)
  • 2021-01-18 05:53

    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

    0 讨论(0)
提交回复
热议问题