Reputation: 887215
I have a pair of static fields with a complicated one-time initialization. I want this initialization to happen lazily, a la the standard singleton pattern.
However, the initialization procedure involves both fields, so I can't separate it into two different singletons.
What's the best way to handle this?
Upvotes: 0
Views: 181
Reputation: 887215
I'm currently doing it like this:
class OuterType {
//...
static class FieldInitializer {
public static readonly SomeType field1, field2;
static FieldInitializer() {
//Complicated code that sets both fields together
}
}
//...
}
Does anyone have any other ideas?
Upvotes: 0
Reputation: 20121
Create a wrapper class that contains the references to both your 'singletons' and make that class the singleton?
Addendum:
If you really want to avoid the second level of indirection with this approach, you can always do it in two stages:
Upvotes: 1