-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Collectionsin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged
Milestone
Description
Background and motivation
If someone wants to return an IList<T>
via a read-only wrapper that prevents the list from being mutated via that reference, they can wrap it in a ReadOnlyCollection<T>
.
If they have an IDictionary<TKey, TValue>
, they can wrap it in a ReadOnlyDictionary<TKey, TValue>
.
But if they have an ISet<T>
, there's no ReadOnlySet<T>
that can be used to easily wrap the set. The dev needs to either write their own wrapper type, and will instead often end up making a copy with an ImmutableHashSet<T>
or FrozenSet<T>
.
API Proposal
namespace System.Collections.ObjectModel;
[DebuggerDisplay("Count = {Count}")]
public class ReadOnlySet<T> : IReadOnlySet<T>, ISet<T>, ICollection
{
public ReadOnlySet(ISet<T> set);
public static ReadOnlySet<T> Empty { get; }
protected ISet<T> Set { get; }
public int Count { get; }
public IEnumerator<T> GetEnumerator();
public bool Contains(T item);
public bool IsProperSubsetOf(IEnumerable<T> other);
public bool IsProperSupersetOf(IEnumerable<T> other);
public bool IsSubsetOf(IEnumerable<T> other);
public bool IsSupersetOf(IEnumerable<T> other);
public bool Overlaps(IEnumerable<T> other);
public bool SetEquals(IEnumerable<T> other);
... // explicit interface implementations of all other interface methods, throwing from mutating members
}
API Usage
HashSet<int> set = ...;
var ros = new ReadOnlySet<int>(set);
Alternative Designs
ISet<T>
exposesCopyTo
whileIReadOnlySet<T>
does not.ReadOnlySet<T>
will implement it, but should it be implicit or explicitly implemented? (Alternatively/separately, should we consider addingCopyTo
toIReadOnlyCollection<T>
and thus implicitly toIReadOnlySet<T>
?)- Do we also want an
ReadOnlySet<T> AsReadOnly()
onHashSet<T>
?List<T>
hasReadOnlyCollection<T> AsReadOnly()
and there's astatic ReadOnlyCollection<T> AsReadOnly(T[])
onArray
, butDictionary<>
doesn't have anAsReadOnly
. - Do we also want an extension method
ReadOnlySet<T> AsReadOnly(this ISet<T>)
? Such an extension exists for bothIList<T>
andIDictionary<>
. - What reference assembly should this be exposed from? ReadOnlyCollection/Dictionary are both implemented in CoreLib and exposed via System.Runtime. Should this live there, too? Or in System.Collections.dll?
Risks
n/a
PaulusParssinen, aromaa, colejohnson66, martincostello, Uladzimir-Lashkevich and 11 more
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedAPI was approved in API review, it can be implementedarea-System.Collectionsin-prThere is an active PR which will close this issue when it is mergedThere is an active PR which will close this issue when it is merged