Best way to prevent a class from being Instantiated?

后端 未结 5 1957
北海茫月
北海茫月 2020-12-14 20:20

I need to know how to prevent a class from being Instantiated in .net?

I know few methods like making the class Abstract and Static.

Is there any more way to

5条回答
  •  Happy的楠姐
    2020-12-14 21:21

    I need to know how to prevent a class from being Instantiated in .net?

    Your question is not clear.

    Do you mean instantiated at runtime? Make the class abstract or static.

    Do you mean that the constructor is not accessible in code? Make the constructor private. But note that someone could still use reflection to grab a handle on a constructor and instantiate an instance at runtime.

    So, which do you mean?

提交回复
热议问题