Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 - Fatal编程技术网

C# 将元素插入多维列表引发索引超出范围异常

C# 将元素插入多维列表引发索引超出范围异常,c#,list,C#,List,我正在处理一个C#list,它是我自己创建的多维类型,我试图从另一个列表中插入元素,由于某些原因,当我试图使用以下代码将值插入0th行时,抛出索引超出范围异常 List<StructuresDS> listStructures = new List<StructuresDS>(); listStructures[0].time =Convert.ToDateTime(AxiomSubSet[0].time.ToString("HH:

我正在处理一个
C#list
,它是我自己创建的多维类型,我试图从另一个列表中插入元素,由于某些原因,当我试图使用以下代码将值插入
0th
行时,抛出索引超出范围异常

List<StructuresDS> listStructures = new List<StructuresDS>();
                    listStructures[0].time =Convert.ToDateTime(AxiomSubSet[0].time.ToString("HH:mm:ss"));
                    listStructures[0].CC = AxiomSubSet[0].CC;
                    listStructures[0].term = listCodedTerms[0];
                    listStructures[0].strike = (Convert.ToDouble(AxiomSubSet[0].strike) * 100).ToString();
                    listStructures[0].strategy = AxiomSubSet[0].strategy;
                    listStructures[0].premium = Convert.ToDouble(AxiomSubSet[0].price);
                    listStructures[0].volume = Convert.ToInt32(AxiomSubSet[0].quantity);
List listStructures=new List();
listStructures[0]。time=Convert.ToDateTime(AxiomSubSet[0]。time.ToString(“HH:mm:ss”);
listStructures[0].CC=AxiomSubSet[0].CC;
listStructures[0].term=listCodedTerms[0];
listStructures[0].strike=(Convert.ToDouble(AxiomSubSet[0].strike)*100).ToString();
listStructures[0]。策略=公理子集[0]。策略;
listStructures[0]。premium=Convert.ToDouble(公理子集[0]。price);
listStructures[0]。volume=Convert.ToInt32(AxiomSubSet[0]。quantity);

我可以知道这种行为的原因以及我犯错误的地方吗?您正在尝试设置
列表结构[0]
,但尚未添加它。您只需执行以下操作:

List<StructuresDS> listStructures = new List<StructuresDS>();
listStructures.Add(new StructureDS 
                   {
                       time = Convert.ToDateTime(AxiomSubSet[0].time.ToString("HH:mm:ss")),
                       CC = AxiomSubSet[0].CC,
                       // etc.
                   });
List listStructures=new List();
添加(新结构)
{
time=Convert.ToDateTime(公理子集[0].time.ToString(“HH:mm:ss”),
CC=公理子集[0]。CC,
//等等。
});

您正在尝试设置
列表结构[0]
,但尚未添加它。您只需执行以下操作:

List<StructuresDS> listStructures = new List<StructuresDS>();
listStructures.Add(new StructureDS 
                   {
                       time = Convert.ToDateTime(AxiomSubSet[0].time.ToString("HH:mm:ss")),
                       CC = AxiomSubSet[0].CC,
                       // etc.
                   });
List listStructures=new List();
添加(新结构)
{
time=Convert.ToDateTime(公理子集[0].time.ToString(“HH:mm:ss”),
CC=公理子集[0]。CC,
//等等。
});

我必须为AxiomSubSet和listCodedTerms插入值,并假设AxiomSubSet.time是一个日期,但在StructuresDS中是一个字符串。我还假设premium和volume分别为double和int&其他所有内容都是字符串,但下面的代码编译和功能与我预期的一样。我认为您的问题在于,您假设在列表实例化之后存在第0个元素,但您没有向其中添加任何内容。一个listStructures.Add(新结构SDS{…})也可以解决这个问题

List<StructuresDS> listStructures = new List<StructuresDS> { 
    new StructuresDS {
                    time = Convert.ToDateTime(AxiomSubSet[0].time.ToString("HH:mm:ss")),
                    CC = AxiomSubSet[0].CC,
                    term = listCodedTerms[0],
                    strike = (Convert.ToDouble(AxiomSubSet[0].strike) * 100).ToString(),
                    strategy =  AxiomSubSet[0].strategy,
                    premium = Convert.ToDouble(AxiomSubSet[0].price),
                    volume =Convert.ToInt32(AxiomSubSet[0].quantity)
                    }
};

当您将它应用到您的环境中时,它对您有效吗?

我必须为AxiomSubSet和listCodedTerms插入值,并假设AxiomSubSet.time是一个日期,但在StructuresDS中是一个字符串。我还假设premium和volume分别为double和int&其他所有内容都是字符串,但下面的代码编译和功能与我预期的一样。我认为您的问题在于,您假设在列表实例化之后存在第0个元素,但您没有向其中添加任何内容。一个listStructures.Add(新结构SDS{…})也可以解决这个问题

List<StructuresDS> listStructures = new List<StructuresDS> { 
    new StructuresDS {
                    time = Convert.ToDateTime(AxiomSubSet[0].time.ToString("HH:mm:ss")),
                    CC = AxiomSubSet[0].CC,
                    term = listCodedTerms[0],
                    strike = (Convert.ToDouble(AxiomSubSet[0].strike) * 100).ToString(),
                    strategy =  AxiomSubSet[0].strategy,
                    premium = Convert.ToDouble(AxiomSubSet[0].price),
                    volume =Convert.ToInt32(AxiomSubSet[0].quantity)
                    }
};

当您将它应用到您的环境中时,它对您有效吗?

listStructures
为空,因此
listStructures[0]中没有元素。
listStructures
为空,因此
listStructures[0]中没有元素
。当我将上面的代码放入循环时,它会添加每一行还是会替换元素?循环并调用
。add()
会在每次迭代时向列表中添加一个新元素。(只需确保第一行在循环之外。)当我将行放入循环时,上面的代码会添加每一行还是会替换元素?循环并调用
。add()
会在每次迭代时向列表中添加一个新元素。(只需确保第一行在循环之外。)