Visibility of nested class constructor

后端 未结 6 892
渐次进展
渐次进展 2021-02-13 18:41

Is there a way to limit the instantiation of the nested class in C#? I want to prevent nested class being instantiated from any other class except the nesting class, but to allo

6条回答
  •  梦毁少年i
    2021-02-13 19:10

    In short, no, you cannot do that. There is an accessibity modifier "public" which means "accessible by anything inside me or outside me" and there is an accessibility modifier "private" which means "accessible by anything inside me". There is no modifier which means "accessible to the thing immediately outside me but not to anything outside it", which is what you would need to mark the constructor as. That's simply not a concept that the designers of the type system thought would be useful.

    Can you describe why you want this crazy kind of accessibility? Perhaps there is a better way to get what you want.

提交回复
热议问题