From 9f921f470f0d6b9c1c732aced97b9922d34f2fdf Mon Sep 17 00:00:00 2001 From: Sergei Papikian <50848091+I-SER-I@users.noreply.github.com> Date: Wed, 3 Feb 2021 19:56:10 +0300 Subject: [PATCH] Added ConcurrentDictionary.Comparer property (#47787) * fix #31350 * fix ref file * fix getter * added tests --- .../ref/System.Collections.Concurrent.cs | 1 + .../Concurrent/ConcurrentDictionary.cs | 2 ++ .../ConcurrentDictionaryTests.cs | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/libraries/System.Collections.Concurrent/ref/System.Collections.Concurrent.cs b/src/libraries/System.Collections.Concurrent/ref/System.Collections.Concurrent.cs index a7955b09120527..7fa61335bf028a 100644 --- a/src/libraries/System.Collections.Concurrent/ref/System.Collections.Concurrent.cs +++ b/src/libraries/System.Collections.Concurrent/ref/System.Collections.Concurrent.cs @@ -82,6 +82,7 @@ public ConcurrentDictionary(System.Collections.Generic.IEqualityComparer? public ConcurrentDictionary(int concurrencyLevel, System.Collections.Generic.IEnumerable> collection, System.Collections.Generic.IEqualityComparer? comparer) { } public ConcurrentDictionary(int concurrencyLevel, int capacity) { } public ConcurrentDictionary(int concurrencyLevel, int capacity, System.Collections.Generic.IEqualityComparer? comparer) { } + public System.Collections.Generic.IEqualityComparer Comparer { get { throw null; } } public int Count { get { throw null; } } public bool IsEmpty { get { throw null; } } public TValue this[TKey key] { get { throw null; } set { } } diff --git a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs index fb49255cfe655c..fc19e9c97b1870 100644 --- a/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs +++ b/src/libraries/System.Collections.Concurrent/src/System/Collections/Concurrent/ConcurrentDictionary.cs @@ -1051,6 +1051,8 @@ public TValue this[TKey key] private static void ThrowKeyNotFoundException(TKey key) => throw new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key.ToString())); + public IEqualityComparer Comparer => _comparer ?? _defaultComparer; + /// /// Gets the number of key/value pairs contained in the . diff --git a/src/libraries/System.Collections.Concurrent/tests/ConcurrentDictionary/ConcurrentDictionaryTests.cs b/src/libraries/System.Collections.Concurrent/tests/ConcurrentDictionary/ConcurrentDictionaryTests.cs index e466a37f920d7a..abc0598004f3be 100644 --- a/src/libraries/System.Collections.Concurrent/tests/ConcurrentDictionary/ConcurrentDictionaryTests.cs +++ b/src/libraries/System.Collections.Concurrent/tests/ConcurrentDictionary/ConcurrentDictionaryTests.cs @@ -666,6 +666,24 @@ void AssertDefaultComparerBehavior(ConcurrentDictionary dic } } + [Fact] + public static void TestComparerGetter() + { + AssertComparerBehavior(null); + AssertComparerBehavior(EqualityComparer.Default); + AssertComparerBehavior(StringComparer.InvariantCulture); + AssertComparerBehavior(StringComparer.OrdinalIgnoreCase); + AssertComparerBehavior(EqualityComparer.Default); + AssertComparerBehavior(EqualityComparer.Default); + + void AssertComparerBehavior(IEqualityComparer comparer) + { + var dc = new ConcurrentDictionary(comparer); + object expected = comparer ?? EqualityComparer.Default; + Assert.Same(expected, dc.Comparer); + } + } + private sealed class EqualityApiSpy : IEquatable { public bool ObjectApiUsed { get; private set; }