12

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.

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.
    }
}

How does the get/set block syntax work if I want my client code to retrieve a number from the array through the property MyNumbers?
How can I send it the right index?
What do I have to initialize?

Ola Ström
  • 4,136
  • 5
  • 22
  • 41
Gasoline
  • 321
  • 1
  • 3
  • 12
  • Are you trying to create an [indexer](http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx) property by any chance? – Serguei Aug 11 '11 at 19:00

6 Answers6

27

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; }
}
Blindy
  • 65,249
  • 10
  • 91
  • 131
  • 2
    I would like to know how it works when I am not using automatic properties and have a field for storing the array. – Gasoline Aug 11 '11 at 19:00
  • Arrays are just a reference type in C# you can cast `int[]` to System.Array` for instance. Without auto-properties you just do `int[] nums; public int[] Nums { get { return nums;} set { nums = value } }`. Mind you if you want to get/set individual elements of the array you need to make an [indexer property](http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx). – Serguei Aug 11 '11 at 19:05
  • 1
    I would suggest also using a private setter. – Joel Coehoorn Aug 11 '11 at 19:07
  • 2
    Thanks, it works! I think I overthought it by thinking how the right index is passed to the MyNumbers property but it was done automaticly. Still lot to learn. Also thank you Serguei. I will look into index properties and see what they can do. – Gasoline Aug 11 '11 at 19:09
  • 3
    It should be noted that Microsoft does not recommend using arrays in properties: https://msdn.microsoft.com/en-us/library/0fss9skc%28v=vs.120%29.aspx – user4593252 Apr 25 '16 at 19:57
  • @MetalPhoenix And for good reason. It exposes private that can be modified without respecting your business rules. Use indexed properties instead. – Undreren Aug 25 '17 at 06:58
15

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 keep the array tamper-proof, the property must return a copy of the array. Typically, users will not understand the adverse performance implications of calling such a property. Specifically, they might use the property as an indexed property.

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

Derek Johnson
  • 927
  • 1
  • 11
  • 15
1

If the number of element in the array is fixed, I would only provide a getter for the array and leave off the setter. You will still be able to assign values to individual elements in the array, but this will prevent someone from swapping the whole array out from under you (or setting it to null. The code would look like this:

class DemoClass
{
    public int[] MyNumbers
    { get; private set; }

    public DemoClass(int elements)
    {
        MyNumbers = new int[elements];
    }
}

If the number of elements are not fixed, then you should use a List<int> rather than an array, and then you definitely want a property with no setter.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0
 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.
        }
    }
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
0

It is called Auto-Implemented Properties . So if you have syntax like

public int[] MyNumbers { get; set; }

C# compiler will automatically create for you backing field. This feature was introduced in C# 3.0, and before that you always had to implement property with backing field.

You can read more at: New C# "Orcas" Language Features: Automatic Properties, Object Initializers, and Collection Initializers

Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
-2
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};
    }
}
Dylan Meador
  • 2,381
  • 1
  • 19
  • 32