Collection<T>.Insert(Int32, T) 方法

定义

将元素插入 Collection<T> 的指定索引处。Inserts an element into the Collection<T> at the specified index.

public:
 virtual void Insert(int index, T item);
public void Insert (int index, T item);
abstract member Insert : int * 'T -> unit
override this.Insert : int * 'T -> unit
Public Sub Insert (index As Integer, item As T)

参数

index
Int32

应插入 item 的从零开始的索引。The zero-based index at which item should be inserted.

item
T

要插入的对象。The object to insert. 对于引用类型,该值可以为 nullThe value can be null for reference types.

实现

例外

index 小于零。index is less than zero.

-or- index 大于 Countindex is greater than Count.

注解

Collection<T> 接受 null 作为引用类型的有效值,并允许重复元素。Collection<T> accepts null as a valid value for reference types and allows duplicate elements.

如果 index 等于 Count,则 item 添加到 Collection<T>的末尾。If index is equal to Count, item is added to the end of Collection<T>.

此方法是一个 O (n)操作,其中 nCount的。This method is an O(n) operation, where n is Count.

继承者说明

派生类可以重写 InsertItem(Int32, T) 来更改此方法的行为。Derived classes can override InsertItem(Int32, T) to change the behavior of this method.

适用于

另请参阅