Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 4e35d33

Browse files
committed
WeakCollection<T>: use Equals() in Remove() implementation. This is necessary to make the use as WeakCollection<EventHandler> work as expected.
1 parent 9722179 commit 4e35d33

2 files changed

Lines changed: 7 additions & 17 deletions

File tree

src/Main/Core/Project/Src/Util/CommandWrapper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ void EnsureCommandCreated()
160160

161161
public event EventHandler CanExecuteChanged {
162162
add {
163+
if (value == null)
164+
return;
163165
if (conditions.Count > 0 && RegisterConditionRequerySuggestedHandler != null)
164166
RegisterConditionRequerySuggestedHandler(value);
165167

@@ -169,6 +171,8 @@ public event EventHandler CanExecuteChanged {
169171
canExecuteChangedHandlersToRegisterOnCommand.Add(value);
170172
}
171173
remove {
174+
if (value == null)
175+
return;
172176
if (conditions.Count > 0 && UnregisterConditionRequerySuggestedHandler != null)
173177
UnregisterConditionRequerySuggestedHandler(value);
174178

src/Main/Core/Project/Src/Util/WeakCollection.cs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,6 @@ public void Clear()
6161
CheckNoEnumerator();
6262
}
6363

64-
/// <summary>
65-
/// Checks if the collection contains an item. Runtime: O(n).
66-
/// </summary>
67-
public bool Contains(T item)
68-
{
69-
if (item == null)
70-
throw new ArgumentNullException("item");
71-
CheckNoEnumerator();
72-
foreach (T element in this) {
73-
if (item.Equals(element))
74-
return true;
75-
}
76-
return false;
77-
}
78-
7964
/// <summary>
8065
/// Removes an element from the collection. Returns true if the item is found and removed,
8166
/// false when the item is not found.
@@ -84,13 +69,14 @@ public bool Contains(T item)
8469
public bool Remove(T item)
8570
{
8671
if (item == null)
87-
throw new ArgumentNullException("item");
72+
return false;
8873
CheckNoEnumerator();
74+
var comparer = EqualityComparer<T>.Default;
8975
for (int i = 0; i < innerList.Count;) {
9076
T element = (T)innerList[i].Target;
9177
if (element == null) {
9278
RemoveAt(i);
93-
} else if (element == item) {
79+
} else if (comparer.Equals(element, item)) {
9480
RemoveAt(i);
9581
return true;
9682
} else {

0 commit comments

Comments
 (0)