首页 > 解决方案 > List.Add 与 List.Insert

问题描述

List 的 Add 和 Insert 操作有什么区别?

List<Tuple<int,string>> samplelinq = new List<Tuple<int, string>>();

在这里,我想列出一个元组。我应该使用添加还是插入。

标签: c#

解决方案


幸运的是,文档解释了Add和之间的区别:Insert

添加

将对象添加到列表的末尾。

签名:

public void Add (T item);

插入

将元素插入到 List 中指定索引处。

签名:

public void Insert (int index, T item);

概括

因此,关于哪个更好的问题的答案完全取决于您要达到的目标。如果您只是想将一个项目添加到列表的末尾,请使用Add.


推荐阅读