Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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#_List_Object - Fatal编程技术网

c#:对象引用未设置为对象的实例

c#:对象引用未设置为对象的实例,c#,list,object,C#,List,Object,这是我代码的一部分 List<DateTime>[] newarraydate1 = new List<DateTime>[70]; DateTime temp1 = arraydate1[k][aa]; newarraydate1[k].Add(temp1); List[]newarraydate1=新列表[70]; DateTime temp1=arraydate1[k][aa]; newarraydate1[k].Add(temp1); 我使用了messageb

这是我代码的一部分

List<DateTime>[] newarraydate1 = new List<DateTime>[70];
DateTime temp1 = arraydate1[k][aa];
newarraydate1[k].Add(temp1);
List[]newarraydate1=新列表[70];
DateTime temp1=arraydate1[k][aa];
newarraydate1[k].Add(temp1);

我使用了messagebox.show(temp1),temp1中有一个值。错误显示在程序的第一行。

您正在声明
列表的数组,但从未在该数组中创建任何实际的列表实例。您需要通过以下方式修改代码:

List<DateTime>[] newarraydate1 = new List<DateTime>[70];
for(int i=0;i<70;i++)
    newarraydate1[i]=new List<DateTime>();
DateTime temp1 = arraydate1[k][aa];
newarraydate1[k].Add(temp1);
List[]newarraydate1=新列表[70];

对于(int i=0;i您正在声明一个
List
数组,但从未在该数组中创建任何实际的List实例。您需要通过以下方式修改代码:

List<DateTime>[] newarraydate1 = new List<DateTime>[70];
for(int i=0;i<70;i++)
    newarraydate1[i]=new List<DateTime>();
DateTime temp1 = arraydate1[k][aa];
newarraydate1[k].Add(temp1);
List[]newarraydate1=新列表[70];

对于(int i=0;i当您创建一个数组时,您只创建包含的结构。它的成员被初始化为默认值,在
List
的情况下,默认值是
null
。基本上,您会得到七十个
null
引用,每个引用都能够保存
DateTime
的列表

要解决此问题,您应该在循环中分配新数组

List<DateTime>[] newarraydate1 = new List<DateTime>[70];
for (int i = 0 ; i != newarraydate1.Length ; i++) {
    newarraydate1[i] = new List<DateTime>();
}
List[]newarraydate1=新列表[70];
for(int i=0;i!=newarraydate1.Length;i++){
newarraydate1[i]=新列表();
}
或者使用LINQ:

List<DateTime>[] newarraydate1 = Enumerable
    .Range(0, 70)
    .Select(n => new List<DateTime>())
    .ToArray();
List[]newarraydate1=可枚举
.范围(0,70)
.Select(n=>newlist())
.ToArray();

当您创建一个数组时,您只创建包含结构。它的成员被初始化为默认值,在
List
null
的情况下,默认值是
null
。基本上,您会得到七十个
null
引用,每个引用都能够保存
DateTime
的列表

要解决此问题,您应该在循环中分配新数组

List<DateTime>[] newarraydate1 = new List<DateTime>[70];
for (int i = 0 ; i != newarraydate1.Length ; i++) {
    newarraydate1[i] = new List<DateTime>();
}
List[]newarraydate1=新列表[70];
for(int i=0;i!=newarraydate1.Length;i++){
newarraydate1[i]=新列表();
}
或者使用LINQ:

List<DateTime>[] newarraydate1 = Enumerable
    .Range(0, 70)
    .Select(n => new List<DateTime>())
    .ToArray();
List[]newarraydate1=可枚举
.范围(0,70)
.Select(n=>newlist())
.ToArray();

如果您有意创建日期时间列表数组,则必须首先在条目中创建每个列表

newarraydate1[k] = new List<DateTime>();
newarraydate1[k]=newlist();

只有这样,您才能
添加到
k
th列表中(如问题的最后一行代码)。

如果是有意创建
DateTime
s的列表,则必须首先使用

newarraydate1[k] = new List<DateTime>();
newarraydate1[k]=newlist();
只有这样,您才能将
添加到
k
th列表中(如问题的最后一行代码)