Properties exposing array elements in C#

Viewed 16319

I want to create a property in C# that sets or returns an individual member of an array. Currently, I have this:

private string[] myProperty;
public string MyProperty[int idx]
{
    get
    {
        if (myProperty == null)
            myProperty = new String[2];

        return myProperty[idx];
    }
    set
    {
        myProperty[idx] = value;
    }
}

However, I get the following compile error:

Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type.

10 Answers
Related