Class TrackingList<T>
Tracks list membership, order, additions, and removals.
public class TrackingList<T> : IList<T>, ICollection<T>, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
Type Parameters
T
- Inheritance
-
TrackingList<T>
- Implements
-
IList<T>ICollection<T>IEnumerable<T>
- Derived
- Inherited Members
- Extension Methods
Constructors
TrackingList(IEnumerable<T>, int, IEqualityComparer<T>?, ITrackingListContext<T>?)
Creates a tracking list with baseline items.
public TrackingList(IEnumerable<T> items, int initialCapacity = 0, IEqualityComparer<T>? comparer = null, ITrackingListContext<T>? context = null)
Parameters
itemsIEnumerable<T>initialCapacityintcomparerIEqualityComparer<T>contextITrackingListContext<T>
TrackingList(int, IEqualityComparer<T>?, ITrackingListContext<T>?)
Creates an empty tracking list.
public TrackingList(int capacity = 0, IEqualityComparer<T>? comparer = null, ITrackingListContext<T>? context = null)
Parameters
capacityintcomparerIEqualityComparer<T>contextITrackingListContext<T>
Properties
Added
Gets the active added items.
public TrackingList<T>.AddedCollection Added { get; }
Property Value
AddedCount
Gets the number of active added items.
public int AddedCount { get; }
Property Value
CanAddNew
Gets whether a new item can be created.
public bool CanAddNew { get; }
Property Value
Capacity
Gets or sets the active item capacity.
public int Capacity { get; set; }
Property Value
Comparer
Gets the item comparer.
public IEqualityComparer<T> Comparer { get; }
Property Value
Context
Gets the confirmation context.
public ITrackingListContext<T> Context { get; }
Property Value
Count
Gets the number of elements contained in the ICollection<T>.
public int Count { get; }
Property Value
- int
The number of elements contained in the ICollection<T>.
HasChanges
Gets whether structural changes exist.
public bool HasChanges { get; }
Property Value
IsReadOnly
Gets a value indicating whether the ICollection<T> is read-only.
public bool IsReadOnly { get; }
Property Value
- bool
true if the ICollection<T> is read-only; otherwise, false.
this[int]
Gets or sets the element at the specified index.
public virtual T this[int index] { get; set; }
Parameters
indexintThe zero-based index of the element to get or set.
Property Value
- T
The element at the specified index.
Exceptions
- ArgumentOutOfRangeException
indexis not a valid index in the IList<T>.- NotSupportedException
The property is set and the IList<T> is read-only.
Removed
Gets the removed items.
public TrackingList<T>.RemovedCollection Removed { get; }
Property Value
RemovedCount
Gets the number of removed items.
public int RemovedCount { get; }
Property Value
RemovedSpan
Gets a span over the removed items.
public ReadOnlySpan<T> RemovedSpan { get; }
Property Value
- ReadOnlySpan<T>
Methods
Add(T)
Adds an item to the ICollection<T>.
public virtual void Add(T item)
Parameters
itemTThe object to add to the ICollection<T>.
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
AddNew()
Creates and adds a new item.
public virtual T AddNew()
Returns
- T
AddTrackedItem(int, T)
Adds an item and returns a token for cancellation.
protected TrackingList<T>.AdditionToken AddTrackedItem(int index, T item)
Parameters
indexintitemT
Returns
AsSpan()
Gets a span over the active items.
public ReadOnlySpan<T> AsSpan()
Returns
- ReadOnlySpan<T>
Clear()
Removes all items from the ICollection<T>.
public virtual void Clear()
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
Confirm(T)
Confirms the active operation for an item.
public bool Confirm(T item)
Parameters
itemT
Returns
ConfirmAddedAt(int)
Confirms an addition and clears list-owned added state on success.
public bool ConfirmAddedAt(int index)
Parameters
indexint
Returns
ConfirmAt(int)
Confirms the row as Added or Edit according to its current structural state.
public bool ConfirmAt(int index)
Parameters
indexint
Returns
ConfirmChanges()
Confirms all observed operations independently.
public bool ConfirmChanges()
Returns
ConfirmDelete(T)
Confirms deletion of a removed item.
public bool ConfirmDelete(T item)
Parameters
itemT
Returns
ConfirmDeleteAt(int)
Confirms deletion of a removed item.
public bool ConfirmDeleteAt(int removedIndex)
Parameters
removedIndexint
Returns
ConfirmEditAt(int)
Confirms the item's edit independently of whether the row is structurally Added.
public bool ConfirmEditAt(int index)
Parameters
indexint
Returns
Contains(T)
Determines whether the ICollection<T> contains a specific value.
public bool Contains(T item)
Parameters
itemTThe object to locate in the ICollection<T>.
Returns
- bool
true if
itemis found in the ICollection<T>; otherwise, false.
CopyAddedTo(Span<T>)
Copies added items to a destination span.
public int CopyAddedTo(Span<T> destination)
Parameters
destinationSpan<T>
Returns
CopyTo(T[], int)
Copies the elements of the ICollection<T> to an Array, starting at a particular Array index.
public void CopyTo(T[] array, int arrayIndex)
Parameters
arrayT[]The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.
arrayIndexintThe zero-based index in
arrayat which copying begins.
Exceptions
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
arrayIndexis less than 0.- ArgumentException
The number of elements in the source ICollection<T> is greater than the available space from
arrayIndexto the end of the destinationarray.
EnsureCapacity(int)
Ensures capacity for active items.
public int EnsureCapacity(int capacity)
Parameters
capacityint
Returns
GetEnumerator()
Gets an active item enumerator.
public TrackingList<T>.Enumerator GetEnumerator()
Returns
IndexOf(T)
Determines the index of a specific item in the IList<T>.
public int IndexOf(T item)
Parameters
itemTThe object to locate in the IList<T>.
Returns
- int
The index of
itemif found in the list; otherwise, -1.
Insert(int, T)
Inserts an item to the IList<T> at the specified index.
public virtual void Insert(int index, T item)
Parameters
indexintThe zero-based index at which
itemshould be inserted.itemTThe object to insert into the IList<T>.
Exceptions
- ArgumentOutOfRangeException
indexis not a valid index in the IList<T>.- NotSupportedException
The IList<T> is read-only.
IsAddedAt(int)
Returns whether the item at an index is added.
public bool IsAddedAt(int index)
Parameters
indexint
Returns
Move(int, int)
Moves an active item.
public void Move(int oldIndex, int newIndex)
Parameters
OnChanged(TrackingListChange<T>)
Handles a structural change.
protected virtual void OnChanged(TrackingListChange<T> change)
Parameters
changeTrackingListChange<T>
OnConfirmed(TrackingListConfirmationKind, int, T)
Handles a confirmed operation.
protected virtual void OnConfirmed(TrackingListConfirmationKind kind, int index, T item)
Parameters
kindTrackingListConfirmationKindindexintitemT
Remove(T)
Removes the first occurrence of a specific object from the ICollection<T>.
public virtual bool Remove(T item)
Parameters
itemTThe object to remove from the ICollection<T>.
Returns
- bool
true if
itemwas successfully removed from the ICollection<T>; otherwise, false. This method also returns false ifitemis not found in the original ICollection<T>.
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
RemoveAt(int)
Removes the IList<T> item at the specified index.
public virtual void RemoveAt(int index)
Parameters
indexintThe zero-based index of the item to remove.
Exceptions
- ArgumentOutOfRangeException
indexis not a valid index in the IList<T>.- NotSupportedException
The IList<T> is read-only.
Restore(T, int)
Restores a removed item.
public bool Restore(T item, int index = -1)
Parameters
itemTindexint
Returns
RestoreAt(int, int)
Restores an observed removal as baseline membership.
public T RestoreAt(int removedIndex, int index = -1)
Parameters
Returns
- T
SetTrackedItem(int, T)
Replaces an item while tracking structural state.
protected void SetTrackedItem(int index, T item)
Parameters
indexintitemT
TrimExcess()
Trims unused active and removed storage.
public void TrimExcess()