Static Generic Class as Dictionary

I like using generic types this way. In particular, I often have private nested generic classes for precisely this purpose.

The main thing I like about it is that it’s hard not to get the initialization right this way (in terms of thread safety), given the way type initialization works. The only problem is what to do if initialization fails – occasionally I’ve resorted to remembering an exception to throw on first necessary access, but that’s pretty rare.

I wouldn’t like to guess at exactly how the CLR looks up the type via the type arguments, but I’m pretty sure it’ll be optimised to heck and back 🙂

Leave a Comment