在 C# 中将元素添加到列表的子列表
Add elements to a sublist of a list in C#
我不知道这个问题是不是太简单了,但我在互联网上没有找到任何解决这个问题的方法。
我有一个列表列表,我在其中添加了三个子列表。
List<List<DateTime>> myList= new List<List<DateTime>>();
while (countNeededSubLists != 0)
{
myList.Add(new List<DateTime>());
countNeededSubLists--;
}
现在我只想将新日期添加到列表索引 1 的子列表中。如何实现,正确的语法是什么?
提前致谢。
myList[1].Add(new DateTime());
我建议您始终检查它是否不为空,如下所示:
if(!myList.Any())
return;
myList.[1].Add(new DateTime());