Alex Rivera | Logout

Array property syntax in C#

Asked 2011-08-11T18:56:11.573
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?

Edit
Report

1 Answer

-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};
    }
}
answered 2011-08-11T19:00:47.107

Your Answer