C# 你能在 List<MyObject> 的位置 0 插入吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1586088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:05:37  来源:igfitidea点击:

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

c#collections

提问by mrblah

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

我需要在集合的开头插入一个对象。

My colleciton is of type List

我的收藏是列表类型

How can I do this?

我怎样才能做到这一点?

采纳答案by Paul Mason

Sure can; for example a generic List of strings:

当然可以;例如一个通用的字符串列表:

List<string> values = new List<string>();
values.Insert(0, "NewString");

回答by Shannon Cornish

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

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

回答by Russ Cam

Take a look at the Insert()method

看一下Insert()方法

myList.Insert(0, [item to insert])

回答by Russ Cam

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 处插入一个项目会将插入的对象放在列表的开头,其他项目将向上移动一个。