13

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?

1
  • Are you trying to create an indexer property by any chance? Commented Aug 11, 2011 at 19:00

6 Answers 6

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; }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I would like to know how it works when I am not using automatic properties and have a field for storing the array.
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.
I would suggest also using a private setter.
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.
It should be noted that Microsoft does not recommend using arrays in properties: msdn.microsoft.com/en-us/library/0fss9skc%28v=vs.120%29.aspx
|
17

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

Comments

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.

Comments

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.