IndexedSet<T> Class
Definition
Represents an index-addressable unique list - an insertion-ordered collection of unique elements that exposes the full IList<T> contract.
public sealed class IndexedSet<T> : IList<T>, ICollection<T>, IReadOnlyList<T>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable where T : notnull
Type Parameters
TThe type of elements in the set. Elements must not be null.
- Inheritance
-
IndexedSet<T>
- Implements
-
IList<T>ICollection<T>IEnumerable<T>
- Inherited Members
- Extension Methods
Examples
// Build a deduplicated playlist that can still be reordered by index.
var playlist = new IndexedSet<string>(StringComparer.OrdinalIgnoreCase);
playlist.Add("Intro");
playlist.Add("Verse");
playlist.Add("Chorus");
bool added = playlist.Add("verse"); // false - already present under case-insensitive comparer
playlist.Insert(0, "Cold Open"); // positional insertion preserves uniqueness
playlist.Move(oldIndex: 1, newIndex: 3);
int chorusIndex = playlist.IndexOf("Chorus"); // O(1) lookup via the hash index
string second = playlist[1]; // O(1) positional read
Remarks
IndexedSet<T> shares its backing storage with OrderedSet<T> via the internal Bodu.Collections.Generic.OrderedSetStorage<T> engine: a contiguous element array for deterministic index order plus an open-addressing hash table for O(1) Contains(T) and IndexOf(T). No BCL collection types are used as backing storage.
Use IndexedSet<T> when callers need positional mutation - Insert(int, T), RemoveAt(int), Move(int, int), or the indexer setter. Use OrderedSet<T> when the public surface is conceptually a set and indices exist only as a read-only view onto insertion order.
This type is not thread-safe.
Constructors
IndexedSet()
Initializes a new instance of the IndexedSet<T> class using the default capacity and comparer.
public IndexedSet()
IndexedSet(IEnumerable<T>)
Initializes a new instance of the IndexedSet<T> class containing the unique elements from
collection.
public IndexedSet(IEnumerable<T> collection)
Parameters
collectionIEnumerable<T>The source collection. Must not be null.
Exceptions
- ArgumentNullException
collectionis null.
IndexedSet(IEnumerable<T>, IEqualityComparer<T>?)
Initializes a new instance of the IndexedSet<T> class containing the unique elements from
collection.
public IndexedSet(IEnumerable<T> collection, IEqualityComparer<T>? comparer)
Parameters
collectionIEnumerable<T>The source collection. Must not be null.
comparerIEqualityComparer<T>The equality comparer, or null to use the default comparer.
Exceptions
- ArgumentNullException
collectionis null.
IndexedSet(IEqualityComparer<T>?)
Initializes a new instance of the IndexedSet<T> class using the specified comparer.
public IndexedSet(IEqualityComparer<T>? comparer)
Parameters
comparerIEqualityComparer<T>The equality comparer, or null to use the default comparer.
IndexedSet(int)
Initializes a new instance of the IndexedSet<T> class with the specified initial capacity.
public IndexedSet(int capacity)
Parameters
capacityintThe initial element capacity.
Exceptions
- ArgumentOutOfRangeException
capacityis negative.
IndexedSet(int, IEqualityComparer<T>?)
Initializes a new instance of the IndexedSet<T> class with the specified initial capacity and comparer.
public IndexedSet(int capacity, IEqualityComparer<T>? comparer)
Parameters
capacityintThe initial element capacity.
comparerIEqualityComparer<T>The equality comparer, or null to use the default comparer.
Exceptions
- ArgumentOutOfRangeException
capacityis negative.
Properties
Capacity
Gets the allocated element capacity.
public int Capacity { get; }
Property Value
- int
The current allocated capacity of the underlying element storage.
Comparer
Gets the equality comparer used to compare elements.
public IEqualityComparer<T> Comparer { get; }
Property Value
- IEqualityComparer<T>
The active equality comparer.
Count
Gets the number of elements in the set.
public int Count { get; }
Property Value
- int
The number of elements currently stored in the set.
IsReadOnly
Gets a value indicating whether the set is read-only.
public bool IsReadOnly { get; }
Property Value
this[int]
Gets or replaces the element at the specified zero-based index.
public T this[int index] { get; set; }
Parameters
indexintThe zero-based index of the element to access.
Property Value
- T
The element at
index.
Exceptions
- ArgumentNullException
The replacement value is null.
- ArgumentOutOfRangeException
indexis negative or greater than or equal to Count.- ArgumentException
The replacement value already exists at another index.
Methods
Add(T)
Adds the specified item to the end of the set.
public bool Add(T item)
Parameters
itemTThe item to add. Must not be null.
Returns
Exceptions
- ArgumentNullException
itemis null.
AddRange(IEnumerable<T>)
Adds each unique item from collection.
public int AddRange(IEnumerable<T> collection)
Parameters
collectionIEnumerable<T>The source collection. Must not be null.
Returns
- int
The number of items added.
Exceptions
- ArgumentNullException
collectionis null.
Clear()
Removes all elements from the set.
public void Clear()
Contains(T)
Determines whether the set contains the specified item.
public bool Contains(T item)
Parameters
itemTThe item to locate. Must not be null.
Returns
Exceptions
- ArgumentNullException
itemis null.
CopyTo(T[], int)
Copies the elements to the specified array starting at arrayIndex.
public void CopyTo(T[] array, int arrayIndex)
Parameters
Exceptions
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
arrayIndexis negative.- ArgumentException
arraydoes not have enough space starting atarrayIndex.
EnsureCapacity(int)
Ensures that the set can hold at least the specified number of items without reallocating storage.
public int EnsureCapacity(int capacity)
Parameters
capacityintThe desired item capacity.
Returns
- int
The resulting item capacity.
Exceptions
- ArgumentOutOfRangeException
capacityis negative.
GetEnumerator()
Returns an enumerator that iterates through the set in insertion order.
public IndexedSet<T>.Enumerator GetEnumerator()
Returns
- IndexedSet<T>.Enumerator
An IndexedSet<T>.Enumerator over the set elements.
IndexOf(T)
Returns the zero-based index of the specified item.
public int IndexOf(T item)
Parameters
itemTThe item to locate. Must not be null.
Returns
- int
The zero-based index of the item, or
-1if it is not present.
Exceptions
- ArgumentNullException
itemis null.
Insert(int, T)
Inserts the specified item at the specified position.
public void Insert(int index, T item)
Parameters
Exceptions
- ArgumentNullException
itemis null.- ArgumentOutOfRangeException
indexis negative or greater than Count.- ArgumentException
The item already exists in the set.
Move(int, int)
Moves an existing element from one index to another.
public void Move(int oldIndex, int newIndex)
Parameters
Exceptions
- ArgumentOutOfRangeException
oldIndexornewIndexis negative or greater than or equal to Count.
Remove(T)
Removes the specified item from the set.
public bool Remove(T item)
Parameters
itemTThe item to remove. Must not be null.
Returns
Exceptions
- ArgumentNullException
itemis null.
RemoveAt(int)
Removes the element at the specified index.
public void RemoveAt(int index)
Parameters
indexintThe zero-based index to remove.
Exceptions
- ArgumentOutOfRangeException
indexis negative or greater than or equal to Count.
ToArray()
Copies the elements to a new array in insertion order.
public T[] ToArray()
Returns
- T[]
A new array containing the set elements.
TrimExcess()
Shrinks the underlying storage to the current element count.
public void TrimExcess()
TryInsert(int, T)
Attempts to insert the specified item at the specified position.
public bool TryInsert(int index, T item)
Parameters
Returns
Exceptions
- ArgumentNullException
itemis null.- ArgumentOutOfRangeException
indexis negative or greater than Count.
Explicit Interface Implementations
ICollection<T>.Add(T)
Adds item via the Add(T) contract.
void ICollection<T>.Add(T item)
Parameters
itemTThe item to add.
Remarks
IEnumerable<T>.GetEnumerator()
Returns an enumerator that iterates through the collection.
IEnumerator<T> IEnumerable<T>.GetEnumerator()
Returns
- IEnumerator<T>
An enumerator that can be used to iterate through the collection.
IEnumerable.GetEnumerator()
Returns an enumerator that iterates through a collection.
IEnumerator IEnumerable.GetEnumerator()
Returns
- IEnumerator
An IEnumerator object that can be used to iterate through the collection.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |