[英]Creating a List<T>, issues with formatting
I'm getting all sorts of compile errors from the code below. 我从下面的代码中得到各种编译错误。 I'm not quite sure how to Add items to a List when I have a List setups as shown below.
当我具有如下所示的“列表”设置时,我不太确定如何向“列表”中添加项目。 I wan't to basically maintain the values set within the StockEntry class, and use the class StockItem as a Count.
我基本上不维护StockEntry类中设置的值,而将StockItem类用作Count。 Using... _stocks.Value.Count++
使用... _stocks.Value.Count ++
public class StockEntry
{
public string Name;
public PeriodType Period;
public int Min;
public StockEntry(string Name, PeriodType Period, int Min)
{
this.Name = Name;
this.Period = Period;
this.Min = Min;
}
}
public class StockItem<T>
{
public T Value;
public StockItem() {}
public StockItem(T val) {Value = val;}
}
List<StockItem<StockEntry>> _stocks = new List<StockItem<StockEntry>>();
protected override void Initialize()
{
_stocks.Add(new StockItem(StockEntry("ABC", PeriodType.Minute, 5)));
_stocks.Add(new StockItem(StockEntry("ACE", PeriodType.Minute, 5)));
_stocks.Add(new StockItem(StockEntry("ACN", PeriodType.Minute, 5)));
}
Aside from anything else, this could be the problem: 除了其他方面,这可能是问题所在:
_stocks.Add(new StockItem(StockEntry("ABC", PeriodType.Minute, 5)));
(and the similar lines). (和类似的行)。
StockItem
is a generic class, so you need to specify the type argument: StockItem
是一个通用类,因此您需要指定type参数:
_stocks.Add(new StockItem<StockEntry>(StockEntry("ABC", PeriodType.Minute, 5)));
If you want to avoid that somewhat redundant specification, you could create a static generic method in a non-generic type: 如果要避免那种多余的规范,则可以创建非泛型类型的静态泛型方法:
// Overload StockItem<T> by generic arity
public static class StockItem
{
public static StockItem<T> Of(T item)
{
return new StockItem<T>(item);
}
}
Then: 然后:
_stocks.Add(StockItem.Of(StockEntry("ABC", PeriodType.Minute, 5)));
That said, it's not really clear why StockItem
needs to be generic in the first place. 就是说,目前尚不清楚为什么
StockItem
需要通用。 And I'd definitely avoid using a public field - at least make it an automatically implemented property. 而且,我绝对会避免使用公共字段-至少使其成为自动实现的属性。 (Ideally IMO, make it a readonly property backed by a readonly field set in the constructor, and remove the parameterless constructor. But then I'm a fan of immutability.) Likewise I'd get rid of the public fields in
StockEntry
. (理想情况下,IMO,使其成为由构造函数中设置的readonly字段支持的readonly属性,并删除无参数构造函数。但是,我是不变性的迷。)同样,我将摆脱
StockEntry
中的公共字段。
I think if you add new
keyword to this line: 我认为,如果您向此行添加
new
关键字:
_stocks.Add(new StockItem(StockEntry("ABC", PeriodType.Minute, 5)));
your problem will be resolve. 您的问题将得到解决。
_stocks.Add(new StockItem(new StockEntry("ABC", PeriodType.Minute, 5)));
_stocks and Initialize need to be part of a class - they are not currently. _stocks和Initialize需要成为一类的一部分-当前不是。
Presumably you want them as part of StockItem: 大概您希望它们作为StockItem的一部分:
public class StockItem<T>
{
public T Value;
public StockItem() {}
public StockItem(T val) {Value = val;}
} // The class StockItem ends with this bracket
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.