如何在ICollection中的特定索引中添加项目?

Joh*_*hen 2 .net collections wpf list c#-4.0

码:

TestItem TI = new TestItem();
ITestItem IC = TI;
controls.TestItems.Add(IC); //This adds the item into the last column, but I need to add this in a particular index

TestItem is a Class  
ITestItem is an Interface 
controls is a local variable
TestItems is a ICollection<ITestItem>
Run Code Online (Sandbox Code Playgroud)

如何在ICollection中的特定索引中添加项目?

Roh*_*ats 5

ICollection<T> does not have insert method 允许在指定的索引位置插入。

相反,您可以使用IList<T>具有插入方法的方法:

void Insert(int index, T item);
Run Code Online (Sandbox Code Playgroud)

您可以这样使用:

controls.TestItems.Add(4, IC);
Run Code Online (Sandbox Code Playgroud)