Class CollectionBase<T>
CollectionBase is a base class that can be used to more easily implement the generic ICollection<T> and non-generic ICollection interfaces.
Inheritance
Implements
Inherited Members
Namespace: Wintellect.PowerCollections
Assembly: CADability.dll
Syntax
[Serializable]
public abstract class CollectionBase<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable
Type Parameters
| Name | Description |
|---|---|
| T | The item type of the collection. |
Remarks
To use CollectionBase as a base class, the derived class must override the Count, GetEnumerator, Add, Clear, and Remove methods.
ICollection<T>.Contains need not be implemented by the derived class, but it should be strongly considered, because the CollectionBase implementation may not be very efficient.
Constructors
| Improve this Doc View SourceCollectionBase()
Creates a new CollectionBase.
Declaration
protected CollectionBase()
Properties
| Improve this Doc View SourceCount
Must be overridden to provide the number of items in the collection.
Declaration
public abstract int Count { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | The number of items in the collection. |
Methods
| Improve this Doc View SourceAdd(T)
Must be overridden to allow adding items to this collection.
Declaration
public virtual void Add(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | Item to be added to the collection. |
Remarks
This method is not abstract, although derived classes should always override it. It is not abstract because some derived classes may wish to reimplement Add with a different return type (typically bool). In C#, this can be accomplished with code like the following:
public class MyCollection<T>: CollectionBase<T>, ICollection<T>
{
public new bool Add(T item) {
/* Add the item */
}
void ICollection<T>.Add(T item) {
Add(item);
}
}</code></pre>
Exceptions
| Type | Condition |
|---|---|
| System.NotImplementedException | Always throws this exception to indicated that the method must be overridden or re-implemented in the derived class. |
AsReadOnly()
Provides a read-only view of this collection. The returned ICollection<T> provides a view of the collection that prevents modifications to the collection. Use the method to provide access to the collection without allowing changes. Since the returned object is just a view, changes to the collection will be reflected in the view.
Declaration
public virtual ICollection<T> AsReadOnly()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.ICollection<T> | An ICollection<T> that provides read-only access to the collection. |
Clear()
Must be overridden to allow clearing this collection.
Declaration
public abstract void Clear()
Contains(T)
Determines if the collection contains a particular item. This default implementation
iterates all of the items in the collection via GetEnumerator, testing each item
against item using IComparable<T>.Equals or
Object.Equals.
Declaration
public virtual bool Contains(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to check for in the collection. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the collection contains |
Remarks
You should strongly consider overriding this method to provide a more efficient implementation, or if the default equality comparison is inappropriate.
ConvertAll<TOutput>(Converter<T, TOutput>)
Convert this collection of items by applying a delegate to each item in the collection. The resulting enumeration
contains the result of applying converter to each item in this collection, in
order.
Declaration
public virtual IEnumerable<TOutput> ConvertAll<TOutput>(Converter<T, TOutput> converter)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Converter<T, TOutput> | converter | A delegate to the method to call, passing each item in this collection. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<TOutput> | An IEnumerable<TOutput^gt; that enumerates the resulting collection from applying |
Type Parameters
| Name | Description |
|---|---|
| TOutput | The type each item is being converted to. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException |
|
CopyTo(T[], Int32)
Copies all the items in the collection into an array. Implemented by using the enumerator returned from GetEnumerator to get all the items and copy them to the provided array.
Declaration
public virtual void CopyTo(T[] array, int arrayIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | Array to copy to. |
| System.Int32 | arrayIndex | Starting index in |
CountWhere(Predicate<T>)
Counts the number of items in the collection that satisfy the condition
defined by predicate.
Declaration
public virtual int CountWhere(Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The number of items in the collection that satisfy |
Exists(Predicate<T>)
Determines if the collection contains any item that satisfies the condition
defined by predicate.
Declaration
public virtual bool Exists(Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the collection contains one or more items that satisfy the condition
defined by |
FindAll(Predicate<T>)
Enumerates the items in the collection that satisfy the condition defined
by predicate.
Declaration
public virtual IEnumerable<T> FindAll(Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<T> | An IEnumerable<T> that enumerates the items that satisfy the condition. |
ForEach(Action<T>)
Performs the specified action on each item in this collection.
Declaration
public virtual void ForEach(Action<T> action)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Action<T> | action | An Action delegate which is invoked for each item in this collection. |
GetEnumerator()
Must be overridden to enumerate all the members of the collection.
Declaration
public abstract IEnumerator<T> GetEnumerator()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerator<T> | A generic IEnumerator<T> that can be used to enumerate all the items in the collection. |
Remove(T)
Must be overridden to allow removing items from this collection.
Declaration
public abstract bool Remove(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if |
RemoveAll(Predicate<T>)
Removes all the items in the collection that satisfy the condition
defined by predicate.
Declaration
public virtual ICollection<T> RemoveAll(Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.ICollection<T> | Returns a collection of the items that were removed, in sorted order. |
ToArray()
Creates an array of the correct size, and copies all the items in the collection into the array, by calling CopyTo.
Declaration
public virtual T[] ToArray()
Returns
| Type | Description |
|---|---|
| T[] | An array containing all the elements in the collection, in order. |
ToString()
Shows the string representation of the collection. The string representation contains a list of the items in the collection. Contained collections (except string) are expanded recursively.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| System.String | The string representation of the collection. |
Overrides
TrueForAll(Predicate<T>)
Determines if all of the items in the collection satisfy the condition
defined by predicate.
Declaration
public virtual bool TrueForAll(Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if all of the items in the collection satisfy the condition
defined by |
Explicit Interface Implementations
| Improve this Doc View SourceICollection<T>.IsReadOnly
Indicates whether the collection is read-only. Always returns false.
Declaration
bool ICollection<T>.IsReadOnly { get; }
Returns
| Type | Description |
|---|---|
| System.Boolean | Always returns false. |
ICollection.CopyTo(Array, Int32)
Copies all the items in the collection into an array. Implemented by using the enumerator returned from GetEnumerator to get all the items and copy them to the provided array.
Declaration
void ICollection.CopyTo(Array array, int index)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Array | array | Array to copy to. |
| System.Int32 | index | Starting index in |
ICollection.IsSynchronized
Indicates whether the collection is synchronized.
Declaration
bool ICollection.IsSynchronized { get; }
Returns
| Type | Description |
|---|---|
| System.Boolean | Always returns false, indicating that the collection is not synchronized. |
ICollection.SyncRoot
Indicates the synchronization object for this collection.
Declaration
object ICollection.SyncRoot { get; }
Returns
| Type | Description |
|---|---|
| System.Object | Always returns this. |
IEnumerable.GetEnumerator()
Provides an IEnumerator that can be used to iterate all the members of the collection. This implementation uses the IEnumerator<T> that was overridden by the derived classes to enumerate the members of the collection.
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
| Type | Description |
|---|---|
| System.Collections.IEnumerator | An IEnumerator that can be used to iterate the collection. |