I have some code that fills two dictionaries
void Load(out Dictionary<string, string> m1, out Dictionary<string, string> m2) { ... }
which I want to execute only when I need access to either of the dictionaries. As there are many methods that rely on the dictionaries being loaded I do not want to pollute all of them by calls to Load
, like this:
void DoSomething()
{
if(notYetInitialized)
Load(out this.dict1, out this.dict2);
// do something with either dict1 or dict2
}
Alternativly I could call the method within my constructor. However this implies Load
is called in any case, regardless if any of the two dctionaries is ever used. That´s why I looked about Lazy
. But as I have one method that should fill two parameters, I´m unsure on how to get this work.
So what I want to achieve is something like this:
void DoSomething()
{
var value = this.dict1[key]; // if dict1 is not yet loaded, call Load, which will also load dict2
}