Area di riferimento
- Developing applications that use system types and collections
- Manage a group of associated data in a .NET Framework application by using collections. (Refer System.Collections namespace)
- CollectionBase class
CollectionBase class
La classe astratta CollectionBase fornisce un punto di partenza per l'implementazione di collection fortemente tipizzate.
Ecco come è definita:
[Serializable, ComVisible(true)]
public abstract class CollectionBase : IList, ICollection, IEnumerable
{
// Fields
private ArrayList list; // Utilizza un ArrayList internamente
// Methods
protected CollectionBase();
protected CollectionBase(int capacity);
public void Clear();
public IEnumerator GetEnumerator();
protected virtual void OnClear();
protected virtual void OnClearComplete();
protected virtual void OnInsert(int index, object value);
protected virtual void OnInsertComplete(int index, object value);
protected virtual void OnRemove(int index, object value);
protected virtual void OnRemoveComplete(int index, object value);
protected virtual void OnSet(int index, object oldValue, object newValue);
protected virtual void OnSetComplete(int index, object oldValue, object newValue);
protected virtual void OnValidate(object value);
public void RemoveAt(int index);
void ICollection.CopyTo(Array array, int index);
int IList.Add(object value);
bool IList.Contains(object value);
int IList.IndexOf(object value);
void IList.Insert(int index, object value);
void IList.Remove(object value);
// Properties
[ComVisible(false)]
public int Capacity { get; set; }
public int Count { get; }
protected ArrayList InnerList { get; }
protected IList List { get; }
bool ICollection.IsSynchronized { get; }
object ICollection.SyncRoot { get; }
bool IList.IsFixedSize { get; }
bool IList.IsReadOnly { get; }
object IList.this[int index] { get; set; }
}
Prelevando un esempio dal sito msdn http://msdn2.microsoft.com/en-us/library/system.collections.collectionbase.aspx vediamo come potrebbe essere implementata
una classe che rappresenta una collection di interi a 16 bit.
public class Int16Collection : CollectionBase
{
public Int16 this[int index]
{
get
{
return ((Int16)List[index]);
}
set
{
List[index] = value;
}
}
public int Add(Int16 value)
{
return (List.Add(value));
}
public int IndexOf(Int16 value)
{
return (List.IndexOf(value));
}
public void Insert(int index, Int16 value)
{
List.Insert(index, value);
}
public void Remove(Int16 value)
{
List.Remove(value);
}
public bool Contains(Int16 value)
{
// If value is not of type Int16, this will return false.
return (List.Contains(value));
}
protected override void OnInsert(int index, Object value)
{
// Insert additional code to be run only when inserting values.
}
protected override void OnRemove(int index, Object value)
{
// Insert additional code to be run only when removing values.
}
protected override void OnSet(int index, Object oldValue, Object newValue)
{
// Insert additional code to be run only when setting values.
}
protected override void OnValidate(Object value)
{
if (value.GetType() != typeof(System.Int16))
throw new ArgumentException("value must be of type Int16.", "value");
}
}
Vedremo in seguito come i generics possono risolvere in modo brillante il problema della creazione di classi fortemente tipizzate.