Q:

C# using indexers

I have a class something like this (This is a subset of the code)
public struct overlay
{
    [FieldOffset(0)]
    public Byte[] b;
    [FieldOffset(0)]
    public Int32[] i;
}

 class MyClass
    {
    private overlay data;    \\Initialised using data.b=new Byte[4096]

    public Int32 site0 { set { data.i[0] = value; } get { return data.i[0]; } }
    public Int32 site1 { set { data.i[1] = value; } get { return data.i[1]; } }
    public String s
    {
        get { return System. Text. Encoding. ASCII.GetString(data.b, 8, 16).TrimEnd(' '); }
        set { System. Text. Encoding. ASCII.GetBytes(value. PadRight(16)).CopyTo(data.b, 8); }
    }
    public Int32 site2 { set { data.i[5] = value; } get { return data.i[5]; } }

    }

I currently access the site variables like this...
MyClass m=new MyClass();
m.site0=1;
m.site1=1;
m.site2=1;

I would like to access them like this..
MyClass m=new MyClass();
for (Int32 i=0; i<m.sites. Count; ++i)
    m.sites[i]=1;

Can anyone suggest how I would do that? 

A:

You can't create indexer for field in your class.  You must make your own class which will store the data and then you can make field of it's type. 
class MyClass
{
    public MyIndexerClass Sites;
    public class MyIndexerClass
    {
        private byte[] data;

        public MyIndexerClass()
        {
            this.data = new byte[0];
        }
        public MyIndexerClass(byte[] Data)
        {
            this.data = Data;
        }

        public byte this[int index]
        {
            get
            {
                return data[index];
            }
            set
            {
                data[index] = value;
            }
        }
    }

    public MyClass()
    {
        this. Sites = new MyIndexerClass();
    }
    public MyClass(byte[] data)
    {
        this. Sites = new MyIndexerClass(data);
    }
}

If you want to use Count property and foreach on MyIndexerClass, you should implement IEnumerable and create your own Enumerator.