Merge pull request #55 from erri120/comparer

Add missing equality comparer parameter and getter
This commit is contained in:
Yoshifumi Kawai 2024-08-20 16:57:46 +09:00 committed by GitHub
commit b0f2a500d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 12 deletions

View File

@ -13,17 +13,17 @@ namespace ObservableCollections
readonly Dictionary<TKey, TValue> dictionary; readonly Dictionary<TKey, TValue> dictionary;
public object SyncRoot { get; } = new object(); public object SyncRoot { get; } = new object();
public ObservableDictionary() public ObservableDictionary(IEqualityComparer<TKey>? comparer = null)
{ {
this.dictionary = new Dictionary<TKey, TValue>(); this.dictionary = new Dictionary<TKey, TValue>(comparer: comparer);
} }
public ObservableDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection) public ObservableDictionary(IEnumerable<KeyValuePair<TKey, TValue>> collection, IEqualityComparer<TKey>? comparer = null)
{ {
#if NET6_0_OR_GREATER #if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
this.dictionary = new Dictionary<TKey, TValue>(collection); this.dictionary = new Dictionary<TKey, TValue>(collection: collection, comparer: comparer);
#else #else
this.dictionary = new Dictionary<TKey, TValue>(); this.dictionary = new Dictionary<TKey, TValue>(comparer: comparer);
foreach (var item in collection) foreach (var item in collection)
{ {
dictionary.Add(item.Key, item.Value); dictionary.Add(item.Key, item.Value);
@ -224,5 +224,16 @@ namespace ObservableCollections
{ {
return GetEnumerator(); return GetEnumerator();
} }
public IEqualityComparer<TKey> Comparer
{
get
{
lock (SyncRoot)
{
return dictionary.Comparer;
}
}
}
} }
} }

View File

@ -14,23 +14,23 @@ namespace ObservableCollections
readonly HashSet<T> set; readonly HashSet<T> set;
public object SyncRoot { get; } = new object(); public object SyncRoot { get; } = new object();
public ObservableHashSet() public ObservableHashSet(IEqualityComparer<T>? comparer = null)
{ {
this.set = new HashSet<T>(); this.set = new HashSet<T>(comparer: comparer);
} }
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER #if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
public ObservableHashSet(int capacity) public ObservableHashSet(int capacity, IEqualityComparer<T>? comparer = null)
{ {
this.set = new HashSet<T>(capacity); this.set = new HashSet<T>(capacity: capacity, comparer: comparer);
} }
#endif #endif
public ObservableHashSet(IEnumerable<T> collection) public ObservableHashSet(IEnumerable<T> collection, IEqualityComparer<T>? comparer = null)
{ {
this.set = new HashSet<T>(collection); this.set = new HashSet<T>(collection: collection, comparer: comparer);
} }
public event NotifyCollectionChangedEventHandler<T>? CollectionChanged; public event NotifyCollectionChangedEventHandler<T>? CollectionChanged;
@ -264,5 +264,16 @@ namespace ObservableCollections
{ {
return GetEnumerator(); return GetEnumerator();
} }
public IEqualityComparer<T> Comparer
{
get
{
lock (SyncRoot)
{
return set.Comparer;
}
}
}
} }
} }