views:

55

answers:

2

I want to inherit from some kind of array/vector/list class so that I can add just one extra specialized method to it.... something like this:

public class SpacesArray : ArrayList<Space>
{
    public Space this[Color c, int i]
    {
        get
        {
            return this[c == Color.White ? i : this.Count - i - 1];
        }
        set
        {
            this[c == Color.White ? i : this.Count - i - 1] = value;
        }
    }
}

But the compiler won't let me. Says

The non-generic type 'System.Collections.ArrayList' cannot be used with type arguments

How can I resolve this?

+4  A: 

ArrayList is not generic. Use List<Space> from System.Collections.Generic.

jdv
Oh... woops! I assumed it had to be.
Mark
+2  A: 

There is no ArrayList<T>. List<T> works rather well instead.

public class SpacesArray : List<Space>
{
    public Space this[Color c, int i]
    {
        get
        {
            return this[c == Color.White ? i : this.Count - i - 1];
        }
        set
        {
            this[c == Color.White ? i : this.Count - i - 1] = value;
        }
    }
}
Igor Zevaka
It seems `List<T>` doesn't work *quite* like an array. You have to add elements to it before you can set them... `this.AddRange(Enumerable.Repeat(Space.Empty, capacity))`. Oh well, works good enough :)
Mark