diff --git a/src/ObservableCollections/ObservableDictionary.cs b/src/ObservableCollections/ObservableDictionary.cs index 897ac7e..118fc87 100644 --- a/src/ObservableCollections/ObservableDictionary.cs +++ b/src/ObservableCollections/ObservableDictionary.cs @@ -3,6 +3,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Linq; namespace ObservableCollections { @@ -18,12 +19,27 @@ namespace ObservableCollections this.dictionary = new Dictionary(); } - public ObservableDictionary(IEnumerable> collection) + public ObservableDictionary(IEqualityComparer? comparer) { -#if NET6_0_OR_GREATER - this.dictionary = new Dictionary(collection); + this.dictionary = new Dictionary(comparer: comparer); + } + + public ObservableDictionary(int capacity, IEqualityComparer? comparer) + { + this.dictionary = new Dictionary(capacity, comparer: comparer); + } + + public ObservableDictionary(IEnumerable> collection) + : this(collection, null) + { + } + + public ObservableDictionary(IEnumerable> collection, IEqualityComparer? comparer) + { +#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER + this.dictionary = new Dictionary(collection: collection, comparer: comparer); #else - this.dictionary = new Dictionary(); + this.dictionary = new Dictionary(comparer: comparer); foreach (var item in collection) { dictionary.Add(item.Key, item.Value); @@ -224,5 +240,16 @@ namespace ObservableCollections { return GetEnumerator(); } + + public IEqualityComparer Comparer + { + get + { + lock (SyncRoot) + { + return dictionary.Comparer; + } + } + } } } diff --git a/src/ObservableCollections/ObservableHashSet.cs b/src/ObservableCollections/ObservableHashSet.cs index 8e2e1d4..0b95615 100644 --- a/src/ObservableCollections/ObservableHashSet.cs +++ b/src/ObservableCollections/ObservableHashSet.cs @@ -19,18 +19,33 @@ namespace ObservableCollections this.set = new HashSet(); } + public ObservableHashSet(IEqualityComparer? comparer) + { + this.set = new HashSet(comparer: comparer); + } + #if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER public ObservableHashSet(int capacity) { - this.set = new HashSet(capacity); + this.set = new HashSet(capacity: capacity); + } + + public ObservableHashSet(int capacity, IEqualityComparer? comparer) + { + this.set = new HashSet(capacity: capacity, comparer: comparer); } #endif public ObservableHashSet(IEnumerable collection) { - this.set = new HashSet(collection); + this.set = new HashSet(collection: collection); + } + + public ObservableHashSet(IEnumerable collection, IEqualityComparer? comparer) + { + this.set = new HashSet(collection: collection, comparer: comparer); } public event NotifyCollectionChangedEventHandler? CollectionChanged; @@ -185,7 +200,7 @@ namespace ObservableCollections public bool TryGetValue(T equalValue, [MaybeNullWhen(false)] out T actualValue) { - lock(SyncRoot) + lock (SyncRoot) { return set.TryGetValue(equalValue, out actualValue); } @@ -264,5 +279,16 @@ namespace ObservableCollections { return GetEnumerator(); } + + public IEqualityComparer Comparer + { + get + { + lock (SyncRoot) + { + return set.Comparer; + } + } + } } } diff --git a/src/ObservableCollections/ObservableQueue.cs b/src/ObservableCollections/ObservableQueue.cs index 2a66ca6..db18b5b 100644 --- a/src/ObservableCollections/ObservableQueue.cs +++ b/src/ObservableCollections/ObservableQueue.cs @@ -171,7 +171,7 @@ namespace ObservableCollections } } - public bool TryPeek([MaybeNullWhen(false)] T result) + public bool TryPeek([MaybeNullWhen(false)] out T result) { lock (SyncRoot) {