OrderedSet<T> Class
Definition
Represents an insertion-ordered set - a ISet<T> that preserves the order in which elements were first added and exposes that order through IReadOnlyList<T>.
public sealed class OrderedSet<T> : ISet<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
-
OrderedSet<T>
- Implements
-
ISet<T>ICollection<T>IEnumerable<T>
- Inherited Members
- Extension Methods
Examples
// De-duplicate a stream of tags while keeping the order in which each was first seen.
var tags = new OrderedSet<string>(StringComparer.OrdinalIgnoreCase);
tags.Add("alpha");
tags.Add("beta");
tags.Add("ALPHA"); // ignored - already present under the case-insensitive comparer
tags.Add("gamma");
Console.WriteLine(string.Join(", ", tags)); // alpha, beta, gamma
Console.WriteLine(tags[0]); // "alpha" - positional read via IReadOnlyList<T>
// Set algebra returns a new OrderedSet preserving the left operand's order.
var diff = new OrderedSet<string>(tags);
diff.ExceptWith(new[] { "beta" });
Remarks
OrderedSet<T> shares its backing storage with IndexedSet<T> via the internal Bodu.Collections.Generic.OrderedSetStorage<T> engine: a contiguous element array for deterministic insertion order plus an open-addressing hash table for O(1) Contains(T). No BCL collection types are used as backing storage.
The contract is strictly set-shaped: ISet<T> for mutation and set algebra,
IReadOnlyList<T> for ordered iteration and positional read access. Positional mutation (Insert
, RemoveAt, Move, indexer setter) is intentionally not exposed - use IndexedSet<T> when
those operations are required.
This type is not thread-safe.
Constructors
OrderedSet()
Initializes a new instance of the OrderedSet<T> class using the default capacity and comparer.
public OrderedSet()
OrderedSet(IEnumerable<T>)
Initializes a new instance of the OrderedSet<T> class containing the unique elements from
collection.
public OrderedSet(IEnumerable<T> collection)
Parameters
collectionIEnumerable<T>The source collection. Must not be null.
Exceptions
- ArgumentNullException
collectionis null.
OrderedSet(IEnumerable<T>, IEqualityComparer<T>?)
Initializes a new instance of the OrderedSet<T> class containing the unique elements from
collection.
public OrderedSet(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.
OrderedSet(IEqualityComparer<T>?)
Initializes a new instance of the OrderedSet<T> class using the specified comparer.
public OrderedSet(IEqualityComparer<T>? comparer)
Parameters
comparerIEqualityComparer<T>The equality comparer, or null to use the default comparer.
OrderedSet(int)
Initializes a new instance of the OrderedSet<T> class with the specified initial capacity.
public OrderedSet(int capacity)
Parameters
capacityintThe initial element capacity.
Exceptions
- ArgumentOutOfRangeException
capacityis negative.
OrderedSet(int, IEqualityComparer<T>?)
Initializes a new instance of the OrderedSet<T> class with the specified initial capacity and comparer.
public OrderedSet(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 the element at the specified zero-based index in insertion order.
public T this[int index] { get; }
Parameters
indexintThe zero-based index of the element to access.
Property Value
- T
The element at
index.
Exceptions
- ArgumentOutOfRangeException
indexis negative or greater than or equal to Count.
Methods
Add(T)
Adds the specified item to 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.
ExceptWith(IEnumerable<T>)
Modifies the current set so that it contains only elements that are not present in other.
public void ExceptWith(IEnumerable<T> other)
Parameters
otherIEnumerable<T>The collection to subtract. Must not be null.
Exceptions
- ArgumentNullException
otheris null.
GetEnumerator()
Returns an enumerator that iterates through the set in insertion order.
public OrderedSet<T>.Enumerator GetEnumerator()
Returns
- OrderedSet<T>.Enumerator
An OrderedSet<T>.Enumerator over the set elements.
IndexOf(T)
Returns the zero-based index of item in insertion order.
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.
IntersectWith(IEnumerable<T>)
Modifies the current set so that it contains only elements that are also present in other.
public void IntersectWith(IEnumerable<T> other)
Parameters
otherIEnumerable<T>The collection to intersect with. Must not be null.
Exceptions
- ArgumentNullException
otheris null.
IsProperSubsetOf(IEnumerable<T>)
Determines whether the current set is a proper (strict) subset of other.
public bool IsProperSubsetOf(IEnumerable<T> other)
Parameters
otherIEnumerable<T>The collection to test against. Must not be null.
Returns
Exceptions
- ArgumentNullException
otheris null.
IsProperSupersetOf(IEnumerable<T>)
Determines whether the current set is a proper (strict) superset of other.
public bool IsProperSupersetOf(IEnumerable<T> other)
Parameters
otherIEnumerable<T>The collection to test against. Must not be null.
Returns
Exceptions
- ArgumentNullException
otheris null.
IsSubsetOf(IEnumerable<T>)
Determines whether the current set is a subset of other.
public bool IsSubsetOf(IEnumerable<T> other)
Parameters
otherIEnumerable<T>The collection to test against. Must not be null.
Returns
Exceptions
- ArgumentNullException
otheris null.
IsSupersetOf(IEnumerable<T>)
Determines whether the current set is a superset of other.
public bool IsSupersetOf(IEnumerable<T> other)
Parameters
otherIEnumerable<T>The collection to test against. Must not be null.
Returns
Exceptions
- ArgumentNullException
otheris null.
Overlaps(IEnumerable<T>)
Determines whether the current set and other share any elements.
public bool Overlaps(IEnumerable<T> other)
Parameters
otherIEnumerable<T>The collection to test against. Must not be null.
Returns
Exceptions
- ArgumentNullException
otheris null.
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.
SetEquals(IEnumerable<T>)
Determines whether the current set contains exactly the same elements as other.
public bool SetEquals(IEnumerable<T> other)
Parameters
otherIEnumerable<T>The collection to compare against. Must not be null.
Returns
- bool
true if the two collections contain the same elements (ignoring duplicates and order); otherwise, false.
Exceptions
- ArgumentNullException
otheris null.
SymmetricExceptWith(IEnumerable<T>)
Modifies the current set so that it contains only elements that are present either in the current set or in
other, but not in both.
public void SymmetricExceptWith(IEnumerable<T> other)
Parameters
otherIEnumerable<T>The collection to apply symmetric difference with. Must not be null.
Exceptions
- ArgumentNullException
otheris null.
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()
UnionWith(IEnumerable<T>)
Modifies the current set so that it contains every element that is present in either this set or
other.
public void UnionWith(IEnumerable<T> other)
Parameters
otherIEnumerable<T>The collection to union with. Must not be null.
Exceptions
- ArgumentNullException
otheris null.
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 |