Possible Duplicate:
Best Practice: Initialize class fields in constructor or at declaration?
I am working with C# but this probably applies to Java (or any other language that allows this behavior as well)...
Which way is preferable/best practice? Assume I will ALWAYS need _memberVar
1.
class MyClass
{
private Dictionary<int, string> _memberVar = new Dictionary<int, string>();
public MyClass() {}
}
- OR -
2.
class MyClass
{
private Dictionary<int, string> _memberVar = null
public MyClass()
{
_memberVar = new Dictionary<int, string>();
}
}
Now lets say that MyClass
has upwards of 10 constructors... So I don't want to have _memberVar = new Dictionary<int, string>();
in all of those constructors lol. Is there anything wrong with the 1st way? Thanks
Edit: I realize I can chain the constructors, but this is a rather complex class... some constructors call the base, some call other constructors already etc etc.