CollectionBase Class
Provides a base class for implementing strongly typed collection classes. This class is abstract.
Syntax
public abstract class CollectionBase : IEnumerable, ICollection, IList
Inheritance Hierarchy
System.Object
→ System.Collections.CollectionBase
Remarks
The CollectionBase
class provides a convenient, abstract base class for implementing custom collection classes. It implements the IList
, ICollection
, and IEnumerable
interfaces. You can derive a custom collection class from CollectionBase
to create a strongly typed collection for your application.
CollectionBase
internally uses an ArrayList
to store the elements of the collection. It provides basic functionality such as adding, removing, and accessing elements. Derived classes typically override methods such as OnInsert
, OnRemove
, and OnSet
to perform custom validation or actions when elements are added, removed, or modified.
Members
Member | Description |
---|---|
CollectionBase() | Initializes a new instance of the CollectionBase class. |
Count | Gets the number of elements contained in the CollectionBase instance. |
InnerList | Gets a ArrayList wrapper around the collection that provides access to the elements. |
Items | Gets the element at the specified index. |
OnClear() | Performs custom processing when the collection is cleared. (Protected) |
OnInsert(int index, object value) | Performs custom processing before inserting a new element. (Protected) |
OnRemove(int index, object value) | Performs custom processing when removing an element. (Protected) |
OnSet(int index, object oldValue, object newValue) | Performs custom processing when replacing an element. (Protected) |
OnValidate(int index, object value) | Performs custom validation when adding or setting an element. (Protected) |
Example
Creating a Custom Collection
The following example demonstrates how to create a custom collection class by deriving from CollectionBase
and overriding some of the protected methods for validation.
using System;
using System.Collections;
// Define a custom collection for integers
public class IntegerCollection : CollectionBase
{
// Override OnInsert to ensure only integers are added
protected override void OnInsert(int index, object value)
{
if (!(value is int))
{
throw new ArgumentException("Only integers can be added to this collection.");
}
base.OnInsert(index, value);
}
// Override OnValidate for additional validation
protected override void OnValidate(int index, object value)
{
if ((int)value < 0)
{
throw new ArgumentException("Values must be non-negative.");
}
base.OnValidate(index, value);
}
// Provide an indexer for easy access to elements
public int this[int index]
{
get { return (int)List[index]; }
set { List[index] = value; }
}
}
public class Program
{
public static void Main(string[] args)
{
IntegerCollection myIntCollection = new IntegerCollection();
try
{
myIntCollection.Add(10);
myIntCollection.Add(25);
myIntCollection.Add(5);
Console.WriteLine($"Collection Count: {myIntCollection.Count}");
Console.WriteLine("Elements in the collection:");
for (int i = 0; i < myIntCollection.Count; i++)
{
Console.WriteLine($" Index {i}: {myIntCollection[i]}");
}
// This will throw an ArgumentException because it's not an integer
// myIntCollection.Add("hello");
// This will throw an ArgumentException because it's negative
// myIntCollection.Add(-2);
}
catch (ArgumentException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Imports System
Imports System.Collections
' Define a custom collection for integers
Public Class IntegerCollection
Inherits CollectionBase
' Override OnInsert to ensure only integers are added
Protected Overrides Sub OnInsert(index As Integer, value As Object)
If Not TypeOf value Is Integer Then
Throw New ArgumentException("Only integers can be added to this collection.")
End If
MyBase.OnInsert(index, value)
End Sub
' Override OnValidate for additional validation
Protected Overrides Sub OnValidate(index As Integer, value As Object)
If CType(value, Integer) < 0 Then
Throw New ArgumentException("Values must be non-negative.")
End If
MyBase.OnValidate(index, value)
End Sub
' Provide an indexer for easy access to elements
Public Property Item(index As Integer) As Integer
Get
Return CType(List(index), Integer)
End Get
Set(value As Integer)
List(index) = value
End Set
End Property
End Class
Public Class Program
Public Shared Sub Main(args As String())
Dim myIntCollection As New IntegerCollection()
Try
myIntCollection.Add(10)
myIntCollection.Add(25)
myIntCollection.Add(5)
Console.WriteLine($"Collection Count: {myIntCollection.Count}")
Console.WriteLine("Elements in the collection:")
For i As Integer = 0 To myIntCollection.Count - 1
Console.WriteLine($" Index {i}: {myIntCollection(i)}")
Next
' This will throw an ArgumentException because it's not an integer
' myIntCollection.Add("hello")
' This will throw an ArgumentException because it's negative
' myIntCollection.Add(-2)
Catch ex As ArgumentException
Console.WriteLine($"Error: {ex.Message}")
End Try
End Sub
End Class
Requirements
- Namespace
System.Collections
- Assembly
- System.dll