Hai*_*vgi 1 c# struct initialization arraylist
我有这个代码:
public struct SmartFilter
{
public int from, to;
public ArrayList collect = new ArrayList();
public long bit;
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
不能在结构中使用实例字段初始值设定项
我尝试不同的方式来克服这个错误,但没有成功,
如何在struct/class中有一个数组列表?
那里有很多问题:
ArrayList这些都没有帮助你......
使用class初始化程序可以正常工作
有一个属性,你可以做懒惰初始化:
public ArrayList Collect {
get { return collect ?? (collect = new ArrayList()); }
}
Run Code Online (Sandbox Code Playgroud)
我会重构为:
public class SmartFilter
{
public int From {get;set;}
public int To {get;set;}
private List<SomeKnownType> collect = new List<SomeKnownType>();
public List<SomeKnownType> Collect { get { return collect; } }
public long Bit {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1443 次 |
| 最近记录: |