diff --git a/src/libraries/System.Collections/ref/System.Collections.Forwards.cs b/src/libraries/System.Collections/ref/System.Collections.Forwards.cs new file mode 100644 index 00000000000000..f2d17d02077c43 --- /dev/null +++ b/src/libraries/System.Collections/ref/System.Collections.Forwards.cs @@ -0,0 +1,7 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// ------------------------------------------------------------------------------ +// Changes to this file must follow the https://aka.ms/api-review process. +// ------------------------------------------------------------------------------ + +[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Collections.ObjectModel.ReadOnlySet<>))] diff --git a/src/libraries/System.Collections/ref/System.Collections.cs b/src/libraries/System.Collections/ref/System.Collections.cs index b63883803728ab..fc46acb8f17d4b 100644 --- a/src/libraries/System.Collections/ref/System.Collections.cs +++ b/src/libraries/System.Collections/ref/System.Collections.cs @@ -582,38 +582,6 @@ void System.Collections.IEnumerator.Reset() { } } } } -namespace System.Collections.ObjectModel -{ - public partial class ReadOnlySet : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlySet, System.Collections.Generic.ISet, System.Collections.ICollection, System.Collections.IEnumerable - { - public ReadOnlySet(System.Collections.Generic.ISet @set) { } - public int Count { get { throw null; } } - public static System.Collections.ObjectModel.ReadOnlySet Empty { get { throw null; } } - protected System.Collections.Generic.ISet Set { get { throw null; } } - bool System.Collections.Generic.ICollection.IsReadOnly { get { throw null; } } - bool System.Collections.ICollection.IsSynchronized { get { throw null; } } - object System.Collections.ICollection.SyncRoot { get { throw null; } } - public bool Contains(T item) { throw null; } - public System.Collections.Generic.IEnumerator GetEnumerator() { throw null; } - public bool IsProperSubsetOf(System.Collections.Generic.IEnumerable other) { throw null; } - public bool IsProperSupersetOf(System.Collections.Generic.IEnumerable other) { throw null; } - public bool IsSubsetOf(System.Collections.Generic.IEnumerable other) { throw null; } - public bool IsSupersetOf(System.Collections.Generic.IEnumerable other) { throw null; } - public bool Overlaps(System.Collections.Generic.IEnumerable other) { throw null; } - public bool SetEquals(System.Collections.Generic.IEnumerable other) { throw null; } - void System.Collections.Generic.ICollection.Add(T item) { } - void System.Collections.Generic.ICollection.Clear() { } - void System.Collections.Generic.ICollection.CopyTo(T[] array, int arrayIndex) { } - bool System.Collections.Generic.ICollection.Remove(T item) { throw null; } - bool System.Collections.Generic.ISet.Add(T item) { throw null; } - void System.Collections.Generic.ISet.ExceptWith(System.Collections.Generic.IEnumerable other) { } - void System.Collections.Generic.ISet.IntersectWith(System.Collections.Generic.IEnumerable other) { } - void System.Collections.Generic.ISet.SymmetricExceptWith(System.Collections.Generic.IEnumerable other) { } - void System.Collections.Generic.ISet.UnionWith(System.Collections.Generic.IEnumerable other) { } - void System.Collections.ICollection.CopyTo(System.Array array, int index) { } - System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } - } -} #endif // !BUILDING_CORELIB_REFERENCE namespace System.Collections.Generic { @@ -627,6 +595,7 @@ public static void InsertRange(this System.Collections.Generic.List list, public static bool Remove(this System.Collections.Generic.IDictionary dictionary, TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) { throw null; } public static bool TryAdd(this System.Collections.Generic.IDictionary dictionary, TKey key, TValue value) { throw null; } public static System.Collections.ObjectModel.ReadOnlyCollection AsReadOnly(this IList list) { throw null; } + public static System.Collections.ObjectModel.ReadOnlySet AsReadOnly(this ISet set) { throw null; } public static System.Collections.ObjectModel.ReadOnlyDictionary AsReadOnly(this IDictionary dictionary) where TKey : notnull { throw null; } } public abstract partial class Comparer : System.Collections.Generic.IComparer, System.Collections.IComparer diff --git a/src/libraries/System.Collections/ref/System.Collections.csproj b/src/libraries/System.Collections/ref/System.Collections.csproj index b0c8a034380a17..18d94174306605 100644 --- a/src/libraries/System.Collections/ref/System.Collections.csproj +++ b/src/libraries/System.Collections/ref/System.Collections.csproj @@ -4,6 +4,7 @@ + diff --git a/src/libraries/System.Collections/src/System.Collections.csproj b/src/libraries/System.Collections/src/System.Collections.csproj index ac718a129a9ad6..8e78d63745bf70 100644 --- a/src/libraries/System.Collections/src/System.Collections.csproj +++ b/src/libraries/System.Collections/src/System.Collections.csproj @@ -20,7 +20,6 @@ - diff --git a/src/libraries/System.Collections/tests/Generic/CollectionExtensionsTests.cs b/src/libraries/System.Collections/tests/Generic/CollectionExtensionsTests.cs index d07e031f0e7acf..013ea47c9b08fd 100644 --- a/src/libraries/System.Collections/tests/Generic/CollectionExtensionsTests.cs +++ b/src/libraries/System.Collections/tests/Generic/CollectionExtensionsTests.cs @@ -116,6 +116,17 @@ public void AsReadOnly_TurnsIListIntoReadOnlyCollection() CollectionAsserts.Equal(list, readOnlyCollection); } + [Fact] + public void AsReadOnly_TurnsISetIntoReadOnlySet() + { + ISet set = new HashSet { "A", "B" }; + ReadOnlySet readOnlySet = set.AsReadOnly(); + Assert.NotNull(readOnlySet); + Assert.NotSame(set, readOnlySet); + Assert.NotSame(readOnlySet, set.AsReadOnly()); + CollectionAsserts.Equal(set, readOnlySet); + } + [Fact] public void AsReadOnly_TurnsIDictionaryIntoReadOnlyDictionary() { @@ -134,6 +145,13 @@ public void AsReadOnly_NullIList_ThrowsArgumentNullException() Assert.Throws("list", () => list.AsReadOnly()); } + [Fact] + public void AsReadOnly_NullISet_ThrowsArgumentNullException() + { + ISet set = null; + AssertExtensions.Throws("set", () => set.AsReadOnly()); + } + [Fact] public void AsReadOnly_NullIDictionary_ThrowsArgumentNullException() { diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index ff2f86e7106faa..3efe4af9fe9a20 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -243,6 +243,7 @@ + diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/CollectionExtensions.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/CollectionExtensions.cs index cf4256f546d59f..6823ab6cfdcaaa 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/CollectionExtensions.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/CollectionExtensions.cs @@ -65,6 +65,17 @@ public static bool Remove(this IDictionary dictionar public static ReadOnlyCollection AsReadOnly(this IList list) => new ReadOnlyCollection(list); + /// + /// Returns a read-only wrapper + /// for the specified set. + /// + /// The type of elements in the set. + /// The set to wrap. + /// An object that acts as a read-only wrapper around the current . + /// is null. + public static ReadOnlySet AsReadOnly(this ISet set) => + new ReadOnlySet(set); + /// /// Returns a read-only wrapper /// for the current dictionary. diff --git a/src/libraries/System.Collections/src/System/Collections/Generic/ReadOnlySet.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/ReadOnlySet.cs similarity index 100% rename from src/libraries/System.Collections/src/System/Collections/Generic/ReadOnlySet.cs rename to src/libraries/System.Private.CoreLib/src/System/Collections/ObjectModel/ReadOnlySet.cs diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index a08103e2de4a05..4c7a34de961e60 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -8470,6 +8470,35 @@ void System.Collections.ICollection.CopyTo(System.Array array, int index) { } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } } } + public partial class ReadOnlySet : System.Collections.Generic.ICollection, System.Collections.Generic.IEnumerable, System.Collections.Generic.IReadOnlyCollection, System.Collections.Generic.IReadOnlySet, System.Collections.Generic.ISet, System.Collections.ICollection, System.Collections.IEnumerable + { + public ReadOnlySet(System.Collections.Generic.ISet @set) { } + public int Count { get { throw null; } } + public static System.Collections.ObjectModel.ReadOnlySet Empty { get { throw null; } } + protected System.Collections.Generic.ISet Set { get { throw null; } } + bool System.Collections.Generic.ICollection.IsReadOnly { get { throw null; } } + bool System.Collections.ICollection.IsSynchronized { get { throw null; } } + object System.Collections.ICollection.SyncRoot { get { throw null; } } + public bool Contains(T item) { throw null; } + public System.Collections.Generic.IEnumerator GetEnumerator() { throw null; } + public bool IsProperSubsetOf(System.Collections.Generic.IEnumerable other) { throw null; } + public bool IsProperSupersetOf(System.Collections.Generic.IEnumerable other) { throw null; } + public bool IsSubsetOf(System.Collections.Generic.IEnumerable other) { throw null; } + public bool IsSupersetOf(System.Collections.Generic.IEnumerable other) { throw null; } + public bool Overlaps(System.Collections.Generic.IEnumerable other) { throw null; } + public bool SetEquals(System.Collections.Generic.IEnumerable other) { throw null; } + void System.Collections.Generic.ICollection.Add(T item) { } + void System.Collections.Generic.ICollection.Clear() { } + void System.Collections.Generic.ICollection.CopyTo(T[] array, int arrayIndex) { } + bool System.Collections.Generic.ICollection.Remove(T item) { throw null; } + bool System.Collections.Generic.ISet.Add(T item) { throw null; } + void System.Collections.Generic.ISet.ExceptWith(System.Collections.Generic.IEnumerable other) { } + void System.Collections.Generic.ISet.IntersectWith(System.Collections.Generic.IEnumerable other) { } + void System.Collections.Generic.ISet.SymmetricExceptWith(System.Collections.Generic.IEnumerable other) { } + void System.Collections.Generic.ISet.UnionWith(System.Collections.Generic.IEnumerable other) { } + void System.Collections.ICollection.CopyTo(System.Array array, int index) { } + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { throw null; } + } } namespace System.ComponentModel {