Visibility of nested class constructor

后端 未结 6 889
渐次进展
渐次进展 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条回答
  •  孤街浪徒
    2021-02-13 19:12

    public class Outer
    {
        public class Nested
        {
            readonly Outer Outer;
    
            public Nested(Outer outer /* , parameters */)
            {
                Outer = outer;
    
                // implementation
            }
    
            // implementation
        }
    
        public Nested GetNested(/* parameters */) => new Nested(this /* , parameters */);
    }
    

    Note that you can access private members of Outer from Nested.

提交回复
热议问题