-2

I'm writing a program that basically creates a list of items. It first stores all the item numbers in a List of strings. After that, after each item, I want the program to store multiple lines of descriptions. Is there a way to add new data between two pre-existing points of data in the List without destroying any pre-existing data? I know in C++ you could push back a Vector, but I'm not sure if you can or how to do the same in C#.

4

2 回答 2

2

您可以使用List<T>.Insert方法:

List<Item> myItems = new List<Item>();
myItems.Add(new Item("1"));
myItems.Insert(0, new Item("2"));

Add用于在列表末尾添加一个对象。Insert用于在指定索引处插入元素。

于 2013-09-13T16:40:59.747 回答
1

如果我在关注,您应该可以只使用泛型。

var yourlist =new List<object>();
foreach (var result in results) { 
    var descriptionlist=new List<stirng>();
    descriptionlist.Add("line1");
    descriptionlist.Add("line2");
    descriptionlist.Add("line3");
    yourlist.Add(new {
        id = result.id,
        description = descriptionlist
    });
}
于 2013-09-13T16:46:26.830 回答