﻿//C#.Net 2008
//Console Application

public struct Row
{
    public int Field1;
    public int Field2;
    public int Field3;
}



public class List
{
    //________________________________________________________________________

    private Row[] m_Buffer;
    private int m_Count;
    //________________________________________________________________________

    public List()
    {
        this.m_Buffer = new Row[1];
        this.m_Count = 0;
    }
    //________________________________________________________________________

    public void Insert(Row value, int index)
    {
        //----- Clamp ...
        if (index < 0) index = 0;
        else if (index > this.m_Count) index = this.m_Count;
        //----- Expand ...
        if (this.m_Count >= this.m_Buffer.Length)
        {
            int capacity = this.m_Buffer.Length;
            Row[] newbuffer = new Row[capacity + capacity];
            
            while (--capacity >= 0)
                newbuffer[capacity] = this.m_Buffer[capacity];

            //System.GC.SuppressFinalize(this.m_Buffer);
            this.m_Buffer = newbuffer;
        }
        //----- Shift ...
        for (int i = this.m_Count; i > index; --i)
        {
            this.m_Buffer[i] = this.m_Buffer[i - 1];
        }
        //----- Set ...
        this.m_Buffer[index] = value;
        this.m_Count++;
    }
    //________________________________________________________________________

    public void Add(Row value)
    {
        this.Insert(value, 0x7fffffff);
    }
    //________________________________________________________________________

    public void RemoveAt(int index)
    {
        if (this.m_Count <= 0) return;
        //----- Clamp ...
        if (index < 0) index = 0;
        else if (index >= this.m_Count) index = this.m_Count - 1;
        //----- Shift ...
        while (++index < this.m_Count)
        {
            this.m_Buffer[index - 1] = this.m_Buffer[index];
        }
        //----- Set ...
        this.m_Count--;
    }
    //________________________________________________________________________

    public int Count
    {
        get { return this.m_Count; }
    }
    //________________________________________________________________________

    public Row this[int index]
    {
        get
        {
            if (this.m_Count <= 0) return new Row();
            if (index < 0) index = 0;
            else if (index >= this.m_Count) index = this.m_Count - 1;

            return this.m_Buffer[index];
        }
        set
        {
            if (this.m_Count <= 0) return;
            if (index < 0) index = 0;
            else if (index >= this.m_Count) index = this.m_Count - 1;

            this.m_Buffer[index] = value;
        }
    }
    //________________________________________________________________________





    [STAThread()] public static void Main()
    {
        List v = new List();
        Row r;

        r.Field1 = 14;
        v.Add(r);

        r = v[0];
        r.Field2 = 114;
        v[0] = r;

        //...

        int iField = Console.Read();
        Console.WriteLine("End");
    }
    //________________________________________________________________________
}
