我有两个私有列表需要在创建对象时初始化.第二个列表取决于第一个列表.我能这样做吗:
public class MyClass
{
private List<T> myList = new List<T>();
private ReadOnlyCollection<T> myReadOnlyList = myList.AsReadOnly;
...
}
Run Code Online (Sandbox Code Playgroud)
第二个列表是第一个只读包装.
我可以期望c#每次运行时都按此顺序执行这两行吗?
或者我应该将这个初始化放在构造函数中?
编辑:
抱歉愚蠢的问题.我试过了,编译器说:
Error 1 A field initializer cannot reference the
non-static field, method, or property...
Run Code Online (Sandbox Code Playgroud)