Class BaseCollection<TItem, TCollection>
- Namespace
- Ecng.Collections
- Assembly
- Ecng.Collections.dll
Represents a base class for a generic collection with event notifications and inner collection management.
public abstract class BaseCollection<TItem, TCollection> : INotifyList<TItem>, INotifyCollection<TItem>, IList<TItem>, ICollection<TItem>, IEnumerable<TItem>, IList, ICollection, IEnumerable where TCollection : ICollection<TItem>
Type Parameters
TItem
The type of elements in the collection.
TCollection
The type of the inner collection, which must implement ICollection<T>.
- Inheritance
-
BaseCollection<TItem, TCollection>
- Implements
-
INotifyList<TItem>INotifyCollection<TItem>IList<TItem>ICollection<TItem>IEnumerable<TItem>
- Derived
- Inherited Members
- Extension Methods
Constructors
BaseCollection(TCollection)
Initializes a new instance of the BaseCollection<TItem, TCollection> class with the specified inner collection.
protected BaseCollection(TCollection innerCollection)
Parameters
innerCollection
TCollectionThe inner collection to manage.
Exceptions
- ArgumentNullException
Thrown when
innerCollection
is null.
Properties
CheckNullableItems
Gets or sets a value indicating whether to check for null items in the collection.
public bool CheckNullableItems { get; set; }
Property Value
Count
Gets the number of items in the collection.
public virtual int Count { get; }
Property Value
InnerCollection
Gets the inner collection that stores the items.
protected TCollection InnerCollection { get; }
Property Value
- TCollection
IsReadOnly
Gets a value indicating whether the collection is read-only.
public virtual bool IsReadOnly { get; }
Property Value
this[int]
Gets or sets the item at the specified index.
public virtual TItem this[int index] { get; set; }
Parameters
index
intThe zero-based index of the item.
Property Value
- TItem
The item at the specified index.
Exceptions
- ArgumentOutOfRangeException
Thrown when the index is invalid.
Methods
Add(TItem)
Adds an item to the collection.
public virtual void Add(TItem item)
Parameters
item
TItemThe item to add.
Exceptions
- ArgumentNullException
Thrown when
item
is null and CheckNullableItems is true.
Clear()
Removes all items from the collection.
public virtual void Clear()
Contains(TItem)
Determines whether the collection contains a specific item.
public virtual bool Contains(TItem item)
Parameters
item
TItemThe item to locate.
Returns
- bool
True if the item is found; otherwise, false.
CopyTo(TItem[], int)
Copies the elements of the collection to an array.
public void CopyTo(TItem[] array, int index)
Parameters
array
TItem[]The destination array.
index
intThe zero-based index in the array at which copying begins.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public virtual IEnumerator<TItem> GetEnumerator()
Returns
- IEnumerator<TItem>
An enumerator for the collection.
IndexOf(TItem)
Returns the index of the specified item in the collection.
public abstract int IndexOf(TItem item)
Parameters
item
TItemThe item to locate.
Returns
- int
The zero-based index of the item, or -1 if not found.
Insert(int, TItem)
Inserts an item into the collection at the specified index.
public virtual void Insert(int index, TItem item)
Parameters
index
intThe zero-based index at which to insert the item.
item
TItemThe item to insert.
Exceptions
- ArgumentOutOfRangeException
Thrown when the index is invalid.
OnAdd(TItem)
Adds an item to the inner collection.
protected virtual void OnAdd(TItem item)
Parameters
item
TItemThe item to add.
OnAdded(TItem)
Invokes the Added event and notifies of a change after adding an item.
protected virtual void OnAdded(TItem item)
Parameters
item
TItemThe added item.
OnAdding(TItem)
Invokes the Adding event before adding an item.
protected virtual bool OnAdding(TItem item)
Parameters
item
TItemThe item to add.
Returns
- bool
True if the addition should proceed; otherwise, false.
OnChanged()
Invokes the Changed event to notify of a modification in the collection.
protected virtual void OnChanged()
OnClear()
Removes all items from the inner collection.
protected virtual void OnClear()
OnCleared()
Invokes the Cleared event and notifies of a change after clearing the collection.
protected virtual void OnCleared()
OnClearing()
Invokes the Clearing event before clearing the collection.
protected virtual bool OnClearing()
Returns
- bool
True if the clearing should proceed; otherwise, false.
OnGetItem(int)
Retrieves an item at the specified index from the inner collection.
protected abstract TItem OnGetItem(int index)
Parameters
index
intThe zero-based index of the item to retrieve.
Returns
- TItem
The item at the specified index.
OnInsert(int, TItem)
Inserts an item into the inner collection at the specified index.
protected abstract void OnInsert(int index, TItem item)
Parameters
index
intThe zero-based index at which to insert the item.
item
TItemThe item to insert.
OnInserted(int, TItem)
Invokes the Inserted event and notifies of a change after inserting an item.
protected virtual void OnInserted(int index, TItem item)
Parameters
index
intThe zero-based index at which the item was inserted.
item
TItemThe inserted item.
OnInserting(int, TItem)
Invokes the Inserting event before inserting an item.
protected virtual bool OnInserting(int index, TItem item)
Parameters
index
intThe zero-based index at which to insert the item.
item
TItemThe item to insert.
Returns
- bool
True if the insertion should proceed; otherwise, false.
OnRemove(TItem)
Removes an item from the inner collection.
protected virtual bool OnRemove(TItem item)
Parameters
item
TItemThe item to remove.
Returns
- bool
True if the item was removed; otherwise, false.
OnRemoveAt(int)
Removes an item from the inner collection at the specified index.
protected abstract void OnRemoveAt(int index)
Parameters
index
intThe zero-based index of the item to remove.
OnRemoved(TItem)
Invokes the Removed event and notifies of a change after removing an item.
protected virtual void OnRemoved(TItem item)
Parameters
item
TItemThe removed item.
OnRemovedAt(int)
Invokes the RemovedAt event and notifies of a change after removing an item at the specified index.
protected virtual void OnRemovedAt(int index)
Parameters
index
intThe zero-based index of the removed item.
OnRemoving(TItem)
Invokes the Removing event before removing an item.
protected virtual bool OnRemoving(TItem item)
Parameters
item
TItemThe item to remove.
Returns
- bool
True if the removal should proceed; otherwise, false.
OnRemovingAt(int)
Invokes the RemovingAt event before removing an item at the specified index.
protected virtual bool OnRemovingAt(int index)
Parameters
index
intThe zero-based index of the item to remove.
Returns
- bool
True if the removal should proceed; otherwise, false.
Remove(TItem)
Removes the specified item from the collection.
public virtual bool Remove(TItem item)
Parameters
item
TItemThe item to remove.
Returns
- bool
True if the item was removed; otherwise, false.
Exceptions
- ArgumentNullException
Thrown when
item
is null and CheckNullableItems is true.
RemoveAt(int)
Removes the item at the specified index.
public virtual void RemoveAt(int index)
Parameters
index
intThe zero-based index of the item to remove.
Events
Added
Occurs after an item has been added to the collection.
public event Action<TItem> Added
Event Type
- Action<TItem>
Adding
Occurs before an item is added to the collection.
public event Func<TItem, bool> Adding
Event Type
Changed
Occurs when the collection has been modified.
public event Action Changed
Event Type
Cleared
Occurs after the collection has been cleared.
public event Action Cleared
Event Type
Clearing
Occurs before the collection is cleared.
public event Func<bool> Clearing
Event Type
Inserted
Occurs after an item has been inserted at the specified index.
public event Action<int, TItem> Inserted
Event Type
Inserting
Occurs before an item is inserted at the specified index.
public event Func<int, TItem, bool> Inserting
Event Type
Removed
Occurs after an item has been removed from the collection.
public event Action<TItem> Removed
Event Type
- Action<TItem>
RemovedAt
Occurs after an item has been removed at the specified index.
public event Action<int> RemovedAt
Event Type
Removing
Occurs before an item is removed from the collection.
public event Func<TItem, bool> Removing
Event Type
RemovingAt
Occurs before an item is removed at the specified index.
public event Func<int, bool> RemovingAt