Generic CollectionBase class

I have a project which requires many custom collections for it’s bussiness layer objects. At first I just implemented ICollection<T> interface for all of them, but as the number of these custom collection classes grew up, the code started to repeat a lot. One possible solution was to use CollectionBase class, but it’s not a generic class so it requires casting, which makes it slower to use. I searched the web for a generic implementation of the CollectionBase class, but the only thing I found was this link: http://msdn.microsoft.com/en-us/library/ms567715.aspx, which wasn’t quite what I want.

As always, when you can’t find what you want on the web, you have to write it on your own. So here’s my CollectionBase<> class. Notice that this implementation is not thread-safe, so if you’ll be using it in multiple threads, be sure to use the SyncRoot property of the collection for locking it.

///
/// Provides a basic abstract generic implementation of ICollection&lt;&gt; and ICollection interfaces.
/// Contains an indexer too.
///
///
public abstract class CollectionBase: ICollection, ICollection
{
	#region Fields

	///
	/// The internal list that the collection items are added to
	///
	protected List internalList;

	#endregion

	#region Constructor

	public CollectionBase()
	{
		this.internalList = new List();
	}

	#endregion

	#region ICollection Members

	///
	/// Adds an item to the collection
	///
	///
The item to add to collection
	public virtual void Add(T item)
	{
		this.internalList.Add(item);
	}

	///
	/// Clears the collection
	///
	public virtual void Clear()
	{
		this.internalList.Clear();
	}

	///
	/// Checks whether given item exists in the collection
	///
	///
Item to search for
	/// True if the item is in collection, false otherwise
	public virtual bool Contains(T item)
	{
		return this.internalList.Contains(item);
	}

	///
	/// Copies the collection items to an array at a given index
	///
	///
Array to copy to
	///
Index in the destination array, at which to start copying to
	public virtual void CopyTo(T[] array, int arrayIndex)
	{
		this.internalList.CopyTo(array, arrayIndex);
	}

	///
	/// Gets the number of items in the collection
	///
	public virtual int Count
	{
		get { return this.internalList.Count; }
	}

	///
	/// Indicates whether the collection is read-only
	///
	public virtual bool IsReadOnly
	{
		get { return false; }
	}

	///
	/// Removes an item from the collection
	///
	///
Item to remove
	/// True if the item was successfully removed, false otherwise
	public virtual bool Remove(T item)
	{
		return this.internalList.Remove(item);
	}

	#endregion

	#region IEnumerable Members

	///
	/// Gets the enumerator for collection items
	///
	///
	public IEnumerator GetEnumerator()
	{
		return this.internalList.GetEnumerator();
	}

	#endregion

	#region IEnumerable Members

	///
	/// Gets the enumerator for collection items
	///
	///
	System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
	{
		return this.internalList.GetEnumerator();
	}

	#endregion

	#region ICollection Members

	///
	/// Copies items from the collection to the array starting at index

	///
	///
The array to copy collection items to
	///
Index in the destination array, at which to start copying
	public virtual void CopyTo(Array array, int index)
	{
		if (array == null)
			throw new ArgumentNullException("array");

		if (array.Length &lt;= index + this.internalList.Count)
			throw new ArgumentOutOfRangeException("The array is too small for the collection items!");

		for (int i = 0; i &lt; this.internalList.Count; i++)
			array.SetValue(this.internalList[i], index + i);
	}

	///
	/// Indicates whether the collection is synchronized
	///
	public virtual bool IsSynchronized
	{
		get { return false; }
	}

	///
	/// Gets the object used for synchronization
	///
	public virtual object SyncRoot
	{
		get { return this; }
	}

	#endregion
}

Now to use the class you just derive it like this:

public class CustomCollection: CollectionBase
{
    // you can override any method you want here
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s