Class CollectionTracker<T>
A utility class that can be used to wrap enumerables to prevent double enumeration. It offers the ability to safely print parts of the collection when failures are encountered, as well as some static versions of the printing functionality.
public sealed class CollectionTracker<T> : CollectionTracker, IDisposable, IEnumerable<T>, IEnumerable
Type Parameters
T
- Inheritance
-
CollectionTracker<T>
- Implements
-
IEnumerable<T>
- Inherited Members
- Extension Methods
Properties
IterationCount
Gets the number of iterations that have happened so far.
public int IterationCount { get; }
Property Value
Methods
Dispose(bool)
Override to provide an implementation of Dispose().
protected override void Dispose(bool disposing)
Parameters
disposingbool
FormatIndexedMismatch(int, int, int?, out int?, int)
Formats the collection when you have a mismatched index. The formatted result will be the section of the
collection from startIndex to endIndex. These indices are usually
obtained by calling GetMismatchExtents(int?, out int, out int).
public override string FormatIndexedMismatch(int startIndex, int endIndex, int? mismatchedIndex, out int? pointerIndent, int depth = 1)
Parameters
startIndexintThe start index of the collection to print
endIndexintThe end index of the collection to print
mismatchedIndexint?The mismatched item index
pointerIndentint?How many spaces into the output value the pointed-to item begins at
depthintThe optional printing depth (1 indicates a top-level value)
Returns
- string
The formatted collection
FormatIndexedMismatch(int?, out int?, int)
Formats the collection when you have a mismatched index. The formatted result will be the section of the collection surrounded by the mismatched item.
public override string FormatIndexedMismatch(int? mismatchedIndex, out int? pointerIndent, int depth = 1)
Parameters
mismatchedIndexint?The index of the mismatched item
pointerIndentint?How many spaces into the output value the pointed-to item begins at
depthintThe optional printing depth (1 indicates a top-level value)
Returns
- string
The formatted collection
FormatIndexedMismatch(ReadOnlySpan<T>, int?, out int?, int)
Formats a span with a mismatched index.
public static string FormatIndexedMismatch(ReadOnlySpan<T> span, int? mismatchedIndex, out int? pointerIndent, int depth = 1)
Parameters
spanReadOnlySpan<T>The span to be formatted
mismatchedIndexint?The mismatched index point
pointerIndentint?How many spaces into the output value the pointed-to item begins at
depthintThe optional printing depth (1 indicates a top-level value)
Returns
- string
The formatted span
FormatStart(IEnumerable<T>, int)
Formats the beginning part of a collection.
public static string FormatStart(IEnumerable<T> collection, int depth = 1)
Parameters
collectionIEnumerable<T>The collection to be formatted
depthintThe optional printing depth (1 indicates a top-level value)
Returns
- string
The formatted collection
FormatStart(int)
Formats the beginning part of the collection.
public override string FormatStart(int depth = 1)
Parameters
depthintThe optional printing depth (1 indicates a top-level value)
Returns
- string
The formatted collection
FormatStart(ReadOnlySpan<T>, int)
Formats the beginning part of a span.
public static string FormatStart(ReadOnlySpan<T> span, int depth = 1)
Parameters
spanReadOnlySpan<T>The span to be formatted
depthintThe optional printing depth (1 indicates a top-level value)
Returns
- string
The formatted span
GetEnumerator()
public IEnumerator<T> GetEnumerator()
Returns
- IEnumerator<T>
GetMismatchExtents(int?, out int, out int)
Gets the extents to print when you find a mismatched index, in the form of
a startIndex and endIndex. If the mismatched
index is null, the extents will start at index 0.
public override void GetMismatchExtents(int? mismatchedIndex, out int startIndex, out int endIndex)
Parameters
mismatchedIndexint?The mismatched item index
startIndexintThe start index that should be used for printing
endIndexintThe end index that should be used for printing
GetSafeEnumerator()
Gets a safe version of IEnumerator that prevents double enumeration and does all the necessary tracking required for collection formatting. Should should be the same value returned by GetEnumerator(), except non-generic.
protected override IEnumerator GetSafeEnumerator()
Returns
TypeAt(int?)
Gets the full name of the type of the element at the given index, if known.
Since this uses the item cache produced by enumeration, it may return null
when we haven't enumerated enough to see the given element, or if we enumerated
so much that the item has left the cache, or if the item at the given index
is null. It will also return null when the index
is null.
public override string? TypeAt(int? index)
Parameters
indexint?The item index
Returns
Wrap(IEnumerable<T>)
Wraps the given collection inside of a CollectionTracker<T>.
public static CollectionTracker<T> Wrap(IEnumerable<T> collection)
Parameters
collectionIEnumerable<T>The collection to be wrapped