Array property syntax in C #

I have a class that has an integer array property, and I'm trying to figure out the correct syntax for it. The int array gets an instance in the class constructor.

class DemoClass { private int[] myNumbers; public int[] MyNumbers { get { /* Some logic */ } set { /* Some logic */ } } public DemoClass(int elements) { // Here, the array should get instantiated using the elements. } } 

Can someone show me the correct code for it to work?

+6
source share
6 answers

You are looking for:

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

As for the usual properties that do nothing but publish a private field (as you think):

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

CA1819: properties should not return arrays

http://msdn.microsoft.com/en-us/library/0fss9skc.aspx

Arrays returned by properties are not write protected, even if the property is read-only. To preserve protection against unauthorized access to the array, the property should return a copy of the array. As a rule, users do not understand the adverse consequences for calling such property. In particular, they can use the property as an indexed property.

To fix a violation of this rule, make the property a method or change the property to return a collection instead of an array

+10
source

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.

0
source
  class DemoClass { private int[] myNumbers; public int[] MyNumbers { get { return myNumbers; } set { myNumbers = value; } } public DemoClass(int[] elements) { myNumbers = elements; // Here, the array should get instantiated using the elements. } } 
0
source

It is called Authorized Properties . Therefore, if you have syntax like

 public int[] MyNumbers { get; set; } 

The C # compiler will automatically create a backup field for you. This feature was introduced in C # 3.0, and before that, you always had to implement the property using the support field.

You can learn more: New features of C # "Orcas" language: automatic properties, object initializers and collection initializers

0
source
 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}; } } 
-1
source

Source: https://habr.com/ru/post/894869/


All Articles