Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中将元素添加到列表的子列表中#_C#_List_Sublist - Fatal编程技术网

C# 在C语言中将元素添加到列表的子列表中#

C# 在C语言中将元素添加到列表的子列表中#,c#,list,sublist,C#,List,Sublist,我不知道这个问题是否太简单,但我还没有在互联网上找到任何解决这个问题的方法 我有一个列表列表,其中添加了三个子列表 List<List<DateTime>> myList= new List<List<DateTime>>(); while (countNeededSubLists != 0) { myList.Add(new List<DateTime>()); countNeededSubLists--; }

我不知道这个问题是否太简单,但我还没有在互联网上找到任何解决这个问题的方法

我有一个列表列表,其中添加了三个子列表

 List<List<DateTime>> myList= new List<List<DateTime>>();

 while (countNeededSubLists != 0)
 {
   myList.Add(new List<DateTime>());
   countNeededSubLists--;
 }
List myList=new List();
while(countneededsublist!=0)
{
添加(新列表());
需要计数的子列表--;
}
现在我只想在列表的索引1处的子列表中添加一个新日期。如何实现这一点,正确的语法是什么


提前谢谢

我建议您始终检查它是否为null,如下所示:

myList[1].Add(new DateTime());
if(!myList.Any())
    return;

myList.[1].Add(new DateTime());

哦,我有一个错误。现在它工作了!非常感谢你!