An obvious singleton implementation for .NET?

后端 未结 7 1146
轻奢々
轻奢々 2020-12-02 11:38

I was thinking about the classic issue of lazy singleton initialization - the whole matter of the inefficiency of:

if (instance == null)
{
    instance = new         


        
7条回答
  •  难免孤独
    2020-12-02 12:13

    the whole matter of the inefficiency of:...

    What inefficiency?

    Those instructions will result into an extremely fast fragments of assembly code. I am completely sure that there is nothing to be gained by trying to "optimize" this. Even if you come up with something faster, it will be at a significant complexity cost.

    Unless you do have positive evidence that this code is affecting your performance, you should use the simplest approach that solves your problem.

提交回复
热议问题