What's the difference in these ways of creating the static instance for a singleton?

前端 未结 2 1734
栀梦
栀梦 2021-02-02 04:06

I have had a bug recently that only manifested itself when the library was built as a release build rather than a debug build. The library is a .NET dll with a COM wrapper and

2条回答
  •  灰色年华
    2021-02-02 04:16

    Just reiterating what Marc Gravell said, but it sounds a lot like a beforefieldinit problem, which means the empty static constructor is your solution. You'd need to post any and all constructors in the class to get a definitive answer.

    The second method has the advantage of lazy loading (where that is an advantage).

提交回复
热议问题