If the number of elements in the array is fixed, I would only provide a getter for the array and break away from the setter. You can still assign values ββto individual elements in the array, but this will not allow any of you to squeeze out the entire array (or set it to null . The code will look like this:
class DemoClass { public int[] MyNumbers { get; private set; } public DemoClass(int elements) { MyNumbers = new int[elements]; } }
If the number of elements is not fixed, you should use a List<int> , not an array, and then you definitely want a property without a setter.
source share