getter and setter for class in class c#

前端 未结 5 1840
攒了一身酷
攒了一身酷 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:35

    The syntax wouldn't be any different. Just...

    public InnerClass InnerClass
    {
        get { return innerClass; }
        set { innerClass = value; }
    }
    

    By the way, if you're using C# in .NET 3.5, you can use the automatic property generation feature if all you have is a simple property that just reads and writes to a backing store (like you have above). The sytax is similar to that of an abstract property:

    public InnerClass InnerClass { get; set; }
    

    This automatically generates a private member for storage, then reads from it in the get and writes to it in the set.

提交回复
热议问题