getter and setter for class in class c#

前端 未结 5 1818
攒了一身酷
攒了一身酷 2021-02-19 01:48

Assuming we have a class InnerClass with attributes and getter/setter. We also have a class OuterClass containing the InnerClass.

e.g.

class InnerClass
{         


        
5条回答
  •  独厮守ぢ
    2021-02-19 02:37

    If you mean accessing inner class members without exposing the inner class itself, you can use the following code. If you just want to expose this.innerClass, there is no difference to the way you expose the fields of InnerClass.

    class OuterClass
    {
        private InnerClass innerClass
    
        public int M_A
        {
            get
            {
                if (this.innerClass != null)
                {
                     return this.innerClass.M_A;
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
            set
            {
                if (this.innerClass != null)
                {
                     this.innerClass.M_A = value;
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
        }
    }
    

提交回复
热议问题