最大数以上のアイテムを追加すると古いものから削除されるリスト
newとかあまり使いたくはないんだけど。すべてをラップするのはめんどいんで。
public class LimitedList<T> : List<T> { private int _maxItemCount; public int MaxItemCount { get { return _maxItemCount; } set { _maxItemCount = value; if ( Count > _maxItemCount ) { RemoveRange( 0, Count - MaxItemCount ); } } } public new void Add( T item ) { base.Add( item ); if ( MaxItemCount != 0 && Count > MaxItemCount ) { RemoveAt( 0 ); } } public new void AddRange( IEnumerable<T> collection ) { base.AddRange( collection ); if ( MaxItemCount != 0 && Count > MaxItemCount ) { RemoveRange( 0, Count - MaxItemCount ); } } public new void Insert(int index, T item) { base.Insert( index, item ); if ( MaxItemCount != 0 && Count > MaxItemCount ) { RemoveAt( 0 ); } } public new void InsertRange( int index, IEnumerable<T> collection ) { base.InsertRange( index, collection ); if ( MaxItemCount != 0 && Count > MaxItemCount ) { RemoveRange( 0, Count - MaxItemCount ); } } }
ついでに
テキストボックスの行数制限とか
public void AddText(string text) { textBox1.Text = textBox1.Text + text; while ( textBox1.Lines.Length > 65535 ) { textBox1.Text = textBox1.Text.Remove( 0, textBox1.Text.IndexOf( "\r\n" ) + 2 ); } }