-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: ReadOnlySet<T> #100113
Labels
api-approved
API was approved in API review, it can be implemented
area-System.Collections
in-pr
There is an active PR which will close this issue when it is merged
Milestone
Comments
Tagging subscribers to this area: @dotnet/area-system-collections |
Should there be an
|
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);
} |
Merged
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
api-approved
API was approved in API review, it can be implemented
area-System.Collections
in-pr
There is an active PR which will close this issue when it is merged
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 aReadOnlyCollection<T>
.If they have an
IDictionary<TKey, TValue>
, they can wrap it in aReadOnlyDictionary<TKey, TValue>
.But if they have an
ISet<T>
, there's noReadOnlySet<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 anImmutableHashSet<T>
orFrozenSet<T>
.Related:
#76028
#29387
#2293
API Proposal
API Usage
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>
?)ReadOnlySet<T> AsReadOnly()
onHashSet<T>
?List<T>
hasReadOnlyCollection<T> AsReadOnly()
and there's astatic ReadOnlyCollection<T> AsReadOnly(T[])
onArray
, butDictionary<>
doesn't have anAsReadOnly
.ReadOnlySet<T> AsReadOnly(this ISet<T>)
? Such an extension exists for bothIList<T>
andIDictionary<>
.Risks
n/a
The text was updated successfully, but these errors were encountered: