public ClassName() {
StringList = new List
}
public string Id { get; set; }
public List
}
A simple, but not thread safe means of instantiating StringList would be…
class ClassName {
private List _StringList;
public ClassName() { }
public string Id { get; set; }
public List {
get { return _StringList ?? (_StringList = new List()); }
set;
}
}
That will get you by in a lot of coding situations, but eventually you may find yourself in a multi-threaded coding situation and need something thread safe…
class ClassName {
private List _StringList;
private object _stringlistlock = new object();
public ClassName() { }
public List {
get {
if (_StringList == null) {
lock(stringlistlock) {
// In case assignment happens between first check and locking call.
if (_StringList == null) {
_StringList = new List();
}
}
}
return _StringList;
}
set;
}
}
Now you're probably wondering why I would have a set option for the property when the get handles the instantiation. It's to support shorthand class instantiation.
Instead of writing…
ClassName cn = new ClassName();
cn.Id = "The Id";
cn.StringList.Add("A");
cn.StringList.Add("B");
cn.StringList.Add("C");
cn.StringList.Add("D");
cn.StringList.Add("E");
…we can write …
ClassName cn = new ClassName() { Id = "The Id", StringList = new List(new string[] { "A", "B", "C", "D", "E" }) }; If you don't like or don't plan to use that class creation shorthand, and you are using DotNet 4 or later, you can use Microsoft's Lazy
Thanks to Stack Overflow, Marc Gravell, and Jim Mischel for some of this information.