2014年6月4日水曜日

Collection クラス

コレクションの雛形なんだけど、実はC# 2.0からあったんだね。

/// <summary>上限付きコレクション</summary>
public class LimitedCollection<T> : Collection<T>
{
    private int limit = int.MaxValue;

    public int Limit
    {
        get
        {
            return this.limit;
        }

        set
        {
            this.limit = value;
            this.CheckLimit();
        }
    }

    private void CheckLimit()
    {
        if (this.Limit <= this.Count)
        {
            throw new InvalidOperationException();
        }
    }

    protected override void SetItem(int index, T item)
    {
        this.CheckLimit();
        base.SetItem(index, item);
    }

    protected override void InsertItem(int index, T item)
    {
        this.CheckLimit();
        base.InsertItem(index, item);
    }
}

0 件のコメント:

コメントを投稿