Array property syntax in C#

后端 未结 6 626
旧巷少年郎
旧巷少年郎 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:50

    class DemoClass
    {
        private int[] myNumbers;
        public int[] MyNumbers
        {
            get { return myNumbers; }
            set { myNumbers = value;}
        }
    
        public DemoClass(int elements)
        {
            // Here, the array should get instantiated using the elements.
            MyNumbers = new int[5] { 1, 2, 3, 4, 5};
        }
    }
    

提交回复
热议问题