我有需要的對象被創建時要進行初始化的兩個私人列表。第二個列表取決於第一個列表。我可以這樣做:初始化私有成員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