C# 如何在不知道值的情况下用特定数量的值实例化列表?

C# 如何在不知道值的情况下用特定数量的值实例化列表?,c#,wpf,livecharts,C#,Wpf,Livecharts,我正在使用wpf的LiveChart,我想实例化一个包含30个值的Decimal列表(就像数组一样),实际上我做了: public SeriesCollection MonthProfit {get;set;} = new SeriesCollection(); int i = 0; MonthProfit[0].Values = new ChartValues<decimal>(); MonthProfit[0].Values[i] = 25; public Series

我正在使用wpf的
LiveChart
,我想实例化一个包含30个值的
Decimal
列表(就像数组一样),实际上我做了:

public SeriesCollection MonthProfit {get;set;} = new SeriesCollection();

 int i = 0;
 MonthProfit[0].Values = new ChartValues<decimal>();
 MonthProfit[0].Values[i] = 25;
public SeriesCollection MonthProfit{get;set;}=new SeriesCollection();
int i=0;
MonthProfit[0]。值=新图表值();
MonthProfit[0]。值[i]=25;
但我得到:

索引超出范围


如何在索引
0
中定义30个值?

在我看来,您可以使用接受
IEnumerable
ChartValues
构造函数:

MonthProfit[0]。值=新图表值(可枚举。重复(0m,30));

我添加了一个答案,假设您在其他地方设置了
MonthProfit
,因此
MonthProfit[0]
实际上存在,并且只有代码的最后一行抛出异常。如果它是
MonthProfit[0]。值=新图表值()
这引发了一个异常,表明您没有在MonthProfit中添加一个系列。(这就是a可以更容易地帮助您的地方-这不需要UI,只是一个完整的示例,显示您已经完成了初始化的哪些方面。)
MonthProfit[0].Values = new ChartValues<decimal>(Enumerable.Repeat(0m, 30));