Array property syntax in C#

后端 未结 6 640
旧巷少年郎
旧巷少年郎 2020-12-18 23:27

I have a a class that has an integer array property and I am trying to figure out the right syntax for it. The integer array gets instantiated in the class constructor.

6条回答
  •  醉酒成梦
    2020-12-18 23:53

    Are you looking for:

    class DemoClass
    {
        public int[] MyNumbers { get; private set; }
    
        public DemoClass(int elements)
        {
            MyNumbers = new int[elements];
        }
    }
    

    As for normal properties that do nothing except publicize a private field (as you seem to want):

    private int[] myNumbers;
    public int[] MyNumbers
    {
        get { return myNumbers; }
        set { myNumbers = value; }
    }
    

提交回复
热议问题