Remove unnecessary null checks from KeyedCollection
authorJustin Van Patten <jvp@justinvp.com>
Fri, 27 Nov 2015 22:46:07 +0000 (14:46 -0800)
committerJustin Van Patten <jvp@justinvp.com>
Fri, 27 Nov 2015 22:46:07 +0000 (14:46 -0800)
Contains and Remove already throw if the key is null, so there's no
need for additional null checks later in these methods.

src/mscorlib/src/System/Collections/ObjectModel/KeyedCollection.cs

index efd8435c49ca61773f0762fa3efc46ffa187be38..98dd6034c6382921afc244ecf3c9ca4f91bca550 100644 (file)
@@ -79,10 +79,8 @@ namespace System.Collections.ObjectModel
                 return dict.ContainsKey(key);
             }
 
-            if (key != null) {
-                foreach (TItem item in Items) {
-                    if (comparer.Equals(GetKeyForItem(item), key)) return true;
-                }
+            foreach (TItem item in Items) {
+                if (comparer.Equals(GetKeyForItem(item), key)) return true;
             }
             return false;
         }
@@ -114,12 +112,10 @@ namespace System.Collections.ObjectModel
                 return false;
             }
 
-            if (key != null) {
-                for (int i = 0; i < Items.Count; i++) {
-                    if (comparer.Equals(GetKeyForItem(Items[i]), key)) {
-                        RemoveItem(i);
-                        return true;
-                    }
+            for (int i = 0; i < Items.Count; i++) {
+                if (comparer.Equals(GetKeyForItem(Items[i]), key)) {
+                    RemoveItem(i);
+                    return true;
                 }
             }
             return false;