2つの初期化のどちらが良いですか?コンストラクタの内部または外部のメンバ初期化
public class ServiceClass
{
private DataManager dataManager = new DataManager();
private Dictionary<string, string> stringDictionary = new Dictionary<string, string>();
private Dictionary<string, DateTime> timeDictionary = new Dictionary<string, DateTime>();
public ServiceClass()
{
//other object creation code
}
}
OR
public class ServiceClass
{
private DataManager dataManager;
private Dictionary<string, string> stringDictionary;
private Dictionary<string, DateTime> timeDictionary;
public ServiceClass()
{
dataManager = new DataManager();
stringDictionary = new Dictionary<string, string>();
timeDictionary = new Dictionary<string, DateTime>();
//other object creation code
}
}
[ベストプラクティス:コンストラクタまたは宣言でクラスフィールドを初期化するかどうかの可能な複製?](http://stackoverflow.com/questions/24551/best-practice-initialize-class-fields-in-constructor-or-at) -宣言) – Gishu