我有需要的对象被创建时要进行初始化的两个私人列表。第二个列表取决于第一个列表。我可以这样做:初始化私有成员C#
public class MyClass
{
private List<T> myList = new List<T>();
private ReadOnlyCollection<T> myReadOnlyList = myList.AsReadOnly;
...
}
第二个列表是只读的第一个包装。
我能想到的是C#将在此为了每次运行时执行该两行呢?
或者我应该把这个初始化在构造函数?
编辑:
对不起,愚蠢的问题。我试了一下,编译说:
Error 1 A field initializer cannot reference the
non-static field, method, or property...
关于您的编辑,请参阅下面的答案:http://stackoverflow.com/questions/2194531/initializing-private-members-c/2194559#2194559。 – jason 2010-02-03 19:04:13