Skip to content

Commit

Permalink
Added ConcurrentDictionary.Comparer property (#47787)
Browse files Browse the repository at this point in the history
* fix #31350

* fix ref file

* fix getter

* added tests
  • Loading branch information
I-SER-I authored Feb 3, 2021
1 parent fabaed2 commit 9f921f4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public ConcurrentDictionary(System.Collections.Generic.IEqualityComparer<TKey>?
public ConcurrentDictionary(int concurrencyLevel, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<TKey, TValue>> collection, System.Collections.Generic.IEqualityComparer<TKey>? comparer) { }
public ConcurrentDictionary(int concurrencyLevel, int capacity) { }
public ConcurrentDictionary(int concurrencyLevel, int capacity, System.Collections.Generic.IEqualityComparer<TKey>? comparer) { }
public System.Collections.Generic.IEqualityComparer<TKey> 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 { } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TKey> Comparer => _comparer ?? _defaultComparer;

/// <summary>
/// Gets the number of key/value pairs contained in the <see
/// cref="ConcurrentDictionary{TKey,TValue}"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,24 @@ void AssertDefaultComparerBehavior(ConcurrentDictionary<EqualityApiSpy, int> dic
}
}

[Fact]
public static void TestComparerGetter()
{
AssertComparerBehavior<string>(null);
AssertComparerBehavior<string>(EqualityComparer<string>.Default);
AssertComparerBehavior<string>(StringComparer.InvariantCulture);
AssertComparerBehavior<string>(StringComparer.OrdinalIgnoreCase);
AssertComparerBehavior<int>(EqualityComparer<int>.Default);
AssertComparerBehavior<bool>(EqualityComparer<bool>.Default);

void AssertComparerBehavior<T>(IEqualityComparer<T> comparer)
{
var dc = new ConcurrentDictionary<T, object>(comparer);
object expected = comparer ?? EqualityComparer<T>.Default;
Assert.Same(expected, dc.Comparer);
}
}

private sealed class EqualityApiSpy : IEquatable<EqualityApiSpy>
{
public bool ObjectApiUsed { get; private set; }
Expand Down

0 comments on commit 9f921f4

Please sign in to comment.