如何使用C#向对象数组添加/添加一些对象,我可以添加静态对象,但是我不知道如何使用动态对象添加对象。
请帮我一个小小的线索,以解决这个问题,谢谢
object dataSource = new object[] {
new { a1 = "111", a11 = "Simpson" },
new { a1 = "Marge ", a11 = "Simpson" },
new { a1 = "Bart", a11 = "Simpson" },
new { a1 = "Lisa", a11 = "Simpson" },
new { a1 = "Maggie", a11 = "Simpson" },
...............
};
答案 0 :(得分:2)
您可以使用array
来调整Array.Resize(ref array, int size)
的大小,但这可能很难管理。一个简单的选择是使用一个列表来管理您的收藏,完成后将其转换为array
。例如:
// define a list
var dataSourceList = new List<object>();
// add items
dataSourceList.Add(new { a1 = "111", a11 = "Simpson" });
dataSourceList.Add(new { a1 = "Marge ", a11 = "Simpson" });
dataSourceList.Add(new { a1 = "Bart", a11 = "Simpson" });
dataSourceList.Add(new { a1 = "Lisa", a11 = "Simpson" });
dataSourceList.Add(new { a1 = "Maggie", a11 = "Simpson" });
...
// convert it to array
var dataSource = dataSourceList.ToArray();