diff --git a/src/J2N/Collections/Generic/Dictionary.cs b/src/J2N/Collections/Generic/Dictionary.cs
index 01622561..41f2064a 100644
--- a/src/J2N/Collections/Generic/Dictionary.cs
+++ b/src/J2N/Collections/Generic/Dictionary.cs
@@ -835,7 +835,7 @@ public bool TryAdd([AllowNull] TKey key, [AllowNull] TValue value) =>
///
/// Getting the value of this property is an O(1) operation.
///
- public KeyCollection Keys => _keys ??= new KeyCollection(this);
+ public ICollection Keys => _keys ??= new KeyCollection(this);
ICollection IDictionary.Keys => Keys;
@@ -854,7 +854,7 @@ public bool TryAdd([AllowNull] TKey key, [AllowNull] TValue value) =>
///
/// Getting the value of this property is an O(1) operation.
///
- public ValueCollection Values => _values ??= new ValueCollection(this);
+ public ICollection Values => _values ??= new ValueCollection(this);
ICollection IDictionary.Values => Values;
@@ -1404,7 +1404,7 @@ public bool Remove([AllowNull] TKey key, [MaybeNullWhen(false)] out TValue value
///
/// This method is an O(1) operation.
///
- public Enumerator GetEnumerator() => new Enumerator(this, Enumerator.KeyValuePair);
+ public IEnumerator> GetEnumerator() => new Enumerator(this, Enumerator.KeyValuePair);
IEnumerator> IEnumerable>.GetEnumerator() =>
Count == 0 ? GenericEmptyEnumerator>.Instance :
@@ -1418,9 +1418,9 @@ IEnumerator> IEnumerable>.
bool IDictionary.IsFixedSize => false;
- ICollection IDictionary.Keys => Keys;
+ ICollection IDictionary.Keys => (ICollection)Keys;
- ICollection IDictionary.Values => Values;
+ ICollection IDictionary.Values => (ICollection)Values;
bool IDictionary.IsReadOnly => false;
diff --git a/src/J2N/Collections/Generic/SortedDictionary.cs b/src/J2N/Collections/Generic/SortedDictionary.cs
index 68ba2726..2db70eec 100644
--- a/src/J2N/Collections/Generic/SortedDictionary.cs
+++ b/src/J2N/Collections/Generic/SortedDictionary.cs
@@ -360,7 +360,7 @@ public IComparer Comparer
///
/// Getting the value of this property is an O(1) operation.
///
- public KeyCollection Keys => _keys ??= new KeyCollection(this);
+ public ICollection Keys => _keys ??= new KeyCollection(this);
ICollection IDictionary.Keys => Keys;
@@ -384,7 +384,7 @@ public IComparer Comparer
///
/// Getting the value of this property is an O(1) operation.
///
- public ValueCollection Values => _values ??= new ValueCollection(this);
+ public ICollection Values => _values ??= new ValueCollection(this);
ICollection IDictionary.Values => Values;
@@ -724,12 +724,12 @@ bool IDictionary.IsReadOnly
ICollection IDictionary.Keys
{
- get { return Keys; }
+ get { return (ICollection)Keys; }
}
ICollection IDictionary.Values
{
- get { return Values; }
+ get { return (ICollection)Values; }
}
object? IDictionary.this[object? key]
@@ -1038,7 +1038,7 @@ public struct Enumerator : IEnumerator>, IDictionaryE
internal Enumerator(SortedDictionary dictionary, int getEnumeratorRetType)
{
- _treeEnum = dictionary._set.GetEnumerator();
+ _treeEnum = dictionary._set.GetEnumeratorInternal();
_getEnumeratorRetType = getEnumeratorRetType;
}
diff --git a/src/J2N/Collections/Generic/SortedSet.cs b/src/J2N/Collections/Generic/SortedSet.cs
index 642b81dd..4899e3e7 100644
--- a/src/J2N/Collections/Generic/SortedSet.cs
+++ b/src/J2N/Collections/Generic/SortedSet.cs
@@ -914,7 +914,9 @@ void ICollection.CopyTo(Array array, int index)
///
/// This method is an O(log n) operation.
///
- public Enumerator GetEnumerator() => new Enumerator(this);
+ public IEnumerator GetEnumerator() => new Enumerator(this);
+
+ internal Enumerator GetEnumeratorInternal() => new Enumerator(this);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
@@ -1203,8 +1205,8 @@ public void UnionWith(IEnumerable other)
// First do a merge sort to an array.
T[] merged = new T[asSorted.Count + this.Count];
int c = 0;
- Enumerator mine = this.GetEnumerator();
- Enumerator theirs = asSorted.GetEnumerator();
+ IEnumerator mine = this.GetEnumerator();
+ IEnumerator theirs = asSorted.GetEnumerator();
bool mineEnded = !mine.MoveNext(), theirsEnded = !theirs.MoveNext();
while (!mineEnded && !theirsEnded)
{
@@ -1359,8 +1361,8 @@ public virtual void IntersectWith(IEnumerable other)
// First do a merge sort to an array.
T[] merged = new T[this.Count];
int c = 0;
- Enumerator mine = this.GetEnumerator();
- Enumerator theirs = asSorted.GetEnumerator();
+ IEnumerator mine = this.GetEnumerator();
+ IEnumerator theirs = asSorted.GetEnumerator();
bool mineEnded = !mine.MoveNext(), theirsEnded = !theirs.MoveNext();
T? max = Max;
@@ -1782,8 +1784,8 @@ public bool SetEquals(IEnumerable other)
SortedSet? asSorted = other as SortedSet;
if (asSorted != null && HasEqualComparer(asSorted))
{
- Enumerator mine = GetEnumerator();
- Enumerator theirs = asSorted.GetEnumerator();
+ IEnumerator mine = GetEnumerator();
+ IEnumerator theirs = asSorted.GetEnumerator();
bool mineEnded = !mine.MoveNext();
bool theirsEnded = !theirs.MoveNext();
while (!mineEnded && !theirsEnded)
diff --git a/tests/J2N.Tests.xUnit/Collections/Generic/Dictionary/Dictionary.Generic.Tests.Keys.cs b/tests/J2N.Tests.xUnit/Collections/Generic/Dictionary/Dictionary.Generic.Tests.Keys.cs
index f304f084..de1b3d47 100644
--- a/tests/J2N.Tests.xUnit/Collections/Generic/Dictionary/Dictionary.Generic.Tests.Keys.cs
+++ b/tests/J2N.Tests.xUnit/Collections/Generic/Dictionary/Dictionary.Generic.Tests.Keys.cs
@@ -75,7 +75,7 @@ public class Dictionary_Generic_Tests_Keys_AsICollection : ICollection_NonGeneri
protected override ICollection NonGenericICollectionFactory()
{
- return new JCG.Dictionary().Keys;
+ return (ICollection)new JCG.Dictionary().Keys;
}
protected override ICollection NonGenericICollectionFactory(int count)
@@ -84,7 +84,7 @@ protected override ICollection NonGenericICollectionFactory(int count)
int seed = 13453;
for (int i = 0; i < count; i++)
list.Add(CreateT(seed++), CreateT(seed++));
- return list.Keys;
+ return (ICollection)list.Keys;
}
private string CreateT(int seed)
diff --git a/tests/J2N.Tests.xUnit/Collections/Generic/Dictionary/Dictionary.Generic.Tests.Values.cs b/tests/J2N.Tests.xUnit/Collections/Generic/Dictionary/Dictionary.Generic.Tests.Values.cs
index 7e019567..aa282eee 100644
--- a/tests/J2N.Tests.xUnit/Collections/Generic/Dictionary/Dictionary.Generic.Tests.Values.cs
+++ b/tests/J2N.Tests.xUnit/Collections/Generic/Dictionary/Dictionary.Generic.Tests.Values.cs
@@ -79,7 +79,7 @@ public class Dictionary_Generic_Tests_Values_AsICollection : ICollection_NonGene
protected override ICollection NonGenericICollectionFactory()
{
- return new JCG.Dictionary().Values;
+ return (ICollection)new JCG.Dictionary().Values;
}
protected override ICollection NonGenericICollectionFactory(int count)
@@ -88,7 +88,7 @@ protected override ICollection NonGenericICollectionFactory(int count)
int seed = 13453;
for (int i = 0; i < count; i++)
list.Add(CreateT(seed++), CreateT(seed++));
- return list.Values;
+ return (ICollection)list.Values;
}
private string CreateT(int seed)