Class ListBase<T>
ListBase is an abstract class that can be used as a base class for a read-write collection that needs to implement the generic IList<T> and non-generic IList collections. The derived class needs to override the following methods: Count, Clear, Insert, RemoveAt, and the indexer. The implementation of all the other methods in IList<T> and IList are handled by ListBase.
Implements
Inherited Members
Namespace: Wintellect.PowerCollections
Assembly: CADability.dll
Syntax
[Serializable]
public abstract class ListBase<T> : CollectionBase<T>, IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
Type Parameters
| Name | Description |
|---|---|
| T |
Constructors
| Improve this Doc View SourceListBase()
Creates a new ListBase.
Declaration
protected ListBase()
Properties
| Improve this Doc View SourceCount
The property must be overridden by the derived class to return the number of items in the list.
Declaration
public abstract override int Count { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | The number of items in the list. |
Overrides
Item[Int32]
The indexer must be overridden by the derived class to get and set values of the list at a particular index.
Declaration
public abstract T this[int index] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The index in the list to get or set an item at. The first item in the list has index 0, and the last has index Count-1. |
Property Value
| Type | Description |
|---|---|
| T | The item at the given index. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
Methods
| Improve this Doc View SourceAdd(T)
Adds an item to the end of the list. This method is equivalent to calling:
Insert(Count, item)
Declaration
public override void Add(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to add to the list. |
Overrides
AsReadOnly()
Provides a read-only view of this list. The returned IList<T> provides a view of the list that prevents modifications to the list. Use the method to provide access to the list without allowing changes. Since the returned object is just a view, changes to the list will be reflected in the view.
Declaration
public virtual IList<T> AsReadOnly()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IList<T> | An IList<T> that provides read-only access to the list. |
Clear()
This method must be overridden by the derived class to empty the list of all items.
Declaration
public abstract override void Clear()
Overrides
Contains(T)
Determines if the list contains any item that compares equal to item.
The implementation simply checks whether IndexOf(item) returns a non-negative value.
Declaration
public override bool Contains(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to search for. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if the list contains an item that compares equal to |
Overrides
Remarks
Equality in the list is determined by the default sense of equality for T. If T implements IComparable<T>, the Equals method of that interface is used to determine equality. Otherwise, Object.Equals is used to determine equality.
CopyTo(T[])
Copies all the items in the list, in order, to array,
starting at index 0.
Declaration
public virtual void CopyTo(T[] array)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to copy to. This array must have a size that is greater than or equal to Count. |
CopyTo(T[], Int32)
Copies all the items in the list, in order, to array,
starting at arrayIndex.
Declaration
public override void CopyTo(T[] array, int arrayIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| T[] | array | The array to copy to. This array must have a size that is greater than or equal to Count + arrayIndex. |
| System.Int32 | arrayIndex | The starting index in |
Overrides
CopyTo(Int32, T[], Int32, Int32)
Copies a range of elements from the list to array,
starting at arrayIndex.
Declaration
public virtual void CopyTo(int index, T[] array, int arrayIndex, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The starting index in the source list of the range to copy. |
| T[] | array | The array to copy to. This array must have a size that is greater than or equal to Count + arrayIndex. |
| System.Int32 | arrayIndex | The starting index in |
| System.Int32 | count | The number of items to copy. |
Find(Predicate<T>)
Finds the first item in the list that satisfies the condition
defined by predicate. If no item matches the condition, than
the default value for T (null or all-zero) is returned.
Declaration
public virtual T Find(Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| T | The first item that satisfies the condition |
Remarks
If the default value for T (null or all-zero) matches the condition defined by predicate,
and the list might contain the default value, then it is impossible to distinguish the different between finding
the default value and not finding any item. To distinguish these cases, use TryFind(Predicate<T>, out T).
See Also
| Improve this Doc View SourceFindIndex(Int32, Int32, Predicate<T>)
Finds the index of the first item, in the range of count items starting from index, that satisfies the condition
defined by predicate. If no item matches the condition, -1 is returned.
Declaration
public virtual int FindIndex(int index, int count, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The starting index of the range to check. |
| System.Int32 | count | The number of items in range to check. |
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item in the given range that satisfies the condition |
FindIndex(Int32, Predicate<T>)
Finds the index of the first item, in the range of items extending from index to the end, that satisfies the condition
defined by predicate. If no item matches the condition, -1 is returned.
Declaration
public virtual int FindIndex(int index, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The starting index of the range to check. |
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item in the given range that satisfies the condition |
FindIndex(Predicate<T>)
Finds the index of the first item in the list that satisfies the condition
defined by predicate. If no item matches the condition, -1 is returned.
Declaration
public virtual int FindIndex(Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item that satisfies the condition |
FindLast(Predicate<T>)
Finds the last item in the list that satisfies the condition
defined by predicate. If no item matches the condition, than
the default value for T (null or all-zero) is returned.
Declaration
public virtual T FindLast(Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| T | The last item that satisfies the condition |
Remarks
If the default value for T (null or all-zero) matches the condition defined by predicate,
and the list might contain the default value, then it is impossible to distinguish the different between finding
the default value and not finding any item. To distinguish these cases, use TryFindLast(Predicate<T>, out T).
See Also
| Improve this Doc View SourceFindLastIndex(Int32, Int32, Predicate<T>)
Finds the index of the last item, in the range of count items ending at index, that satisfies the condition
defined by predicate. If no item matches the condition, -1 is returned.
Declaration
public virtual int FindLastIndex(int index, int count, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The ending index of the range to check. |
| System.Int32 | count | The number of items in range to check. |
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item in the given range that satisfies the condition |
FindLastIndex(Int32, Predicate<T>)
Finds the index of the last item, in the range of items extending from the beginning
of the list to index, that satisfies the condition
defined by predicate. If no item matches the condition, -1 is returned.
Declaration
public virtual int FindLastIndex(int index, Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The ending index of the range to check. |
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item in the given range that satisfies the condition |
FindLastIndex(Predicate<T>)
Finds the index of the last item in the list that satisfies the condition
defined by predicate. If no item matches the condition, -1 is returned.
Declaration
public virtual int FindLastIndex(Predicate<T> predicate)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | predicate | A delegate that defined the condition to check for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item that satisfies the condition |
GetEnumerator()
Enumerates all of the items in the list, in order. The item at index 0 is enumerated first, then the item at index 1, and so on.
Declaration
public override IEnumerator<T> GetEnumerator()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerator<T> | An IEnumerator<T> that enumerates all the items in the list. |
Overrides
Remarks
The enumerator does not check for changes made to the structure of the list. Thus, changes to the list during enumeration may cause incorrect enumeration or out of range exceptions. Consider overriding this method and adding checks for structural changes.
IndexOf(T)
Finds the index of the first item in the list that is equal to item.
Declaration
public virtual int IndexOf(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to search fror. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item in the list that that is equal to |
Remarks
The default implementation of equality for type T is used in the search. This is the equality defined by IComparable<T> or object.Equals.
IndexOf(T, Int32)
Finds the index of the first item, in the range of items extending from index to the end,
that is equal to item.
Declaration
public virtual int IndexOf(T item, int index)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to search fror. |
| System.Int32 | index | The starting index of the range to check. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item in the given range that that is equal to |
Remarks
The default implementation of equality for type T is used in the search. This is the equality defined by IComparable<T> or object.Equals.
IndexOf(T, Int32, Int32)
Finds the index of the first item, in the range of count items starting from index,
that is equal to item.
Declaration
public virtual int IndexOf(T item, int index, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to search fror. |
| System.Int32 | index | The starting index of the range to check. |
| System.Int32 | count | The number of items in range to check. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the first item in the given range that that is equal to |
Remarks
The default implementation of equality for type T is used in the search. This is the equality defined by IComparable<T> or object.Equals.
Insert(Int32, T)
This method must be overridden by the derived class to insert a new item at the given index.
Declaration
public abstract void Insert(int index, T item)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The index in the list to insert the item at. After the insertion, the inserted item is located at this index. The first item in the list has index 0. |
| T | item | The item to insert at the given index. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
LastIndexOf(T)
Finds the index of the last item in the list that is equal to item.
Declaration
public virtual int LastIndexOf(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to search fror. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item in the list that that is equal to |
Remarks
The default implementation of equality for type T is used in the search. This is the equality defined by IComparable<T> or object.Equals.
LastIndexOf(T, Int32)
Finds the index of the last item, in the range of items extending from the beginning
of the list to index, that is equal to item.
Declaration
public virtual int LastIndexOf(T item, int index)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to search fror. |
| System.Int32 | index | The ending index of the range to check. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item in the given range that that is equal to |
Remarks
The default implementation of equality for type T is used in the search. This is the equality defined by IComparable<T> or object.Equals.
LastIndexOf(T, Int32, Int32)
Finds the index of the last item, in the range of count items ending at index,
that is equal to item.
Declaration
public virtual int LastIndexOf(T item, int index, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to search for. |
| System.Int32 | index | The ending index of the range to check. |
| System.Int32 | count | The number of items in range to check. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of the last item in the given range that that is equal to |
Remarks
The default implementation of equality for type T is used in the search. This is the equality defined by IComparable<T> or object.Equals.
Range(Int32, Int32)
Returns a view onto a sub-range of this list. Items are not copied; the returned IList<T> is simply a different view onto the same underlying items. Changes to this list are reflected in the view, and vice versa. Insertions and deletions in the view change the size of the view, but insertions and deletions in the underlying list do not.
Declaration
public virtual IList<T> Range(int start, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | start | The starting index of the view. |
| System.Int32 | count | The number of items in the view. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IList<T> | A list that is a view onto the given sub-part of this list. |
Remarks
This method can be used to apply an algorithm to a portion of a list. For example:
Algorithms.ReverseInPlace(deque.Range(3, 6))
will reverse the 6 items beginning at index 3.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentOutOfRangeException |
|
Remove(T)
Searches the list for the first item that compares equal to item.
If one is found, it is removed. Otherwise, the list is unchanged.
Declaration
public override bool Remove(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to remove from the list. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if an item was found and removed that compared equal to
|
Overrides
Remarks
Equality in the list is determined by the default sense of equality for T. If T implements IComparable<T>, the Equals method of that interface is used to determine equality. Otherwise, Object.Equals is used to determine equality.
RemoveAt(Int32)
This method must be overridden by the derived class to remove the item at the given index.
Declaration
public abstract void RemoveAt(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The index in the list to remove the item at. The first item in the list has index 0. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
TryFind(Predicate<T>, out T)
Finds the first item in the list that satisfies the condition
defined by predicate.
Declaration
public virtual bool TryFind(Predicate<T> predicate, out T foundItem)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
| T | foundItem | If true is returned, this parameter receives the first item in the list
that satifies the condition defined by |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if an item that satisfies the condition |
TryFindLast(Predicate<T>, out T)
Finds the last item in the list that satisfies the condition
defined by predicate.
Declaration
public virtual bool TryFindLast(Predicate<T> predicate, out T foundItem)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | predicate | A delegate that defines the condition to check for. |
| T | foundItem | If true is returned, this parameter receives the last item in the list
that satifies the condition defined by |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if an item that satisfies the condition |
Explicit Interface Implementations
| Improve this Doc View SourceIList.Add(Object)
Adds an item to the end of the list. This method is equivalent to calling:
Insert(Count, item)
Declaration
int IList.Add(object value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | value | The item to add to the list. |
Returns
| Type | Description |
|---|---|
| System.Int32 |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
|
IList.Clear()
Removes all the items from the list, resulting in an empty list.
Declaration
void IList.Clear()
IList.Contains(Object)
Determines if the list contains any item that compares equal to value.
Declaration
bool IList.Contains(object value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | value | The item to search for. |
Returns
| Type | Description |
|---|---|
| System.Boolean |
Remarks
Equality in the list is determined by the default sense of equality for T. If T implements IComparable<T>, the Equals method of that interface is used to determine equality. Otherwise, Object.Equals is used to determine equality.
IList.IndexOf(Object)
Find the first occurrence of an item equal to value
in the list, and returns the index of that item.
Declaration
int IList.IndexOf(object value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | value | The item to search for. |
Returns
| Type | Description |
|---|---|
| System.Int32 | The index of |
Remarks
Equality in the list is determined by the default sense of equality for T. If T implements IComparable<T>, the Equals method of that interface is used to determine equality. Otherwise, Object.Equals is used to determine equality.
IList.Insert(Int32, Object)
Insert a new item at the given index.
Declaration
void IList.Insert(int index, object value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The index in the list to insert the item at. After the insertion, the inserted item is located at this index. The first item in the list has index 0. |
| System.Object | value | The item to insert at the given index. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentException |
|
IList.IsFixedSize
Returns whether the list is a fixed size. This implementation always returns false.
Declaration
bool IList.IsFixedSize { get; }
Returns
| Type | Description |
|---|---|
| System.Boolean | Alway false, indicating that the list is not fixed size. |
IList.IsReadOnly
Returns whether the list is read only. This implementation returns the value from ICollection<T>.IsReadOnly, which is by default, false.
Declaration
bool IList.IsReadOnly { get; }
Returns
| Type | Description |
|---|---|
| System.Boolean | By default, false, indicating that the list is not read only. |
IList.Item[Int32]
Gets or sets the value at a particular index in the list.
Declaration
object IList.this[int index] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The index in the list to get or set an item at. The first item in the list has index 0, and the last has index Count-1. |
Returns
| Type | Description |
|---|---|
| System.Object | The item at the given index. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|
| System.ArgumentException |
|
IList.Remove(Object)
Searches the list for the first item that compares equal to value.
If one is found, it is removed. Otherwise, the list is unchanged.
Declaration
void IList.Remove(object value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | value | The item to remove from the list. |
Remarks
Equality in the list is determined by the default sense of equality for T. If T implements IComparable<T>, the Equals method of that interface is used to determine equality. Otherwise, Object.Equals is used to determine equality.
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
|
IList.RemoveAt(Int32)
Removes the item at the given index.
Declaration
void IList.RemoveAt(int index)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | The index in the list to remove the item at. The first item in the list has index 0. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException |
|