Lazy-loading a Dictionary's Value Collection ?
-
Code example (uses .NET 4.7):
public enum Divisions
{
Undefined,
Admin,
Finance,
Sales,
Research,
IT,
Temp
}// 'PersonNode code not shown here: it's just wrapper around a POCO
public Dictionary>> DivisionToStaff
{ get; set; } = new Dictionary>>();void Initialize()
{
foreach (Divisions div in Enum.GetValues(typeof(Divisions)))
{
DivisionToStaff.Add(div, new Lazy>());
}
}// sample usage : note access is via Lazy 'Value property DivisionToStaff[node.Division].Value.Add(personnode);
Here's my question: I wonder why I have to use 'new in the initialization code for the lazy-loaded lists in the Dictionary Values: I thought I could just set the Values to 'null ? Out of curiosity is there a zen-like way to make the entire Dictionary lazy-loaded ? thanks, Bill
«When I consider my brief span of life, swallowed up in an eternity before and after, the little space I fill, and even can see, engulfed in the infinite immensity of spaces of which I am ignorant, and which know me not, I am frightened, and am astonished at being here rather than there; for there is no reason why here rather than there, now rather than then.» Blaise Pascal