[Solved] Array property syntax in C#

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?

Enquirer: Gasoline

||

Solution #1:

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; }
}
Respondent: Blindy

Solution #2:

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

Respondent: Derek Johnson

Solution #3:

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.

Respondent: Joel Coehoorn

Solution #4:

 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.
        }
    }
Respondent: Haris Hasan

Solution #5:

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

Respondent: Vlad Bezden

Solution #6:

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};
    }
}
Respondent: Dylan Meador

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.