What is the need of private constructor in C#?

前端 未结 12 1537
鱼传尺愫
鱼传尺愫 2020-12-13 21:32

What is the need of private constructor in C#? I got it as a question for a C# test.

12条回答
  •  天命终不由人
    2020-12-13 21:59

    I can can recall few usages for it:

    • You could use it from a static factory method inside the same class
    • You could do some common work inside it and then call it from other contructure
    • You could use it to prevent the runtime from adding an empty contructure automatically
    • It could be used (although private) from some mocking and ORM tools (like nhibernate)

提交回复
热议问题