Merge pull request #1069 from esdrubal/bug16990
authorMarek Safar <marek.safar@gmail.com>
Fri, 30 May 2014 11:43:02 +0000 (13:43 +0200)
committerMarek Safar <marek.safar@gmail.com>
Fri, 30 May 2014 11:43:02 +0000 (13:43 +0200)
Changed ConcurrentDictionary.Contains(KeyValuePair) to also compare value. Fixes #16990.

mcs/class/corlib/System.Collections.Concurrent/ConcurrentDictionary.cs
mcs/class/corlib/Test/System.Collections.Concurrent/ConcurrentDictionaryTests.cs

index e5a26ce28d3577b53d240dec40c7b0e96d189f9e..8098cdbc5828bfd6e7cd188e3bab68dd4127b619 100644 (file)
@@ -262,7 +262,11 @@ namespace System.Collections.Concurrent
 
                bool ICollection<KeyValuePair<TKey,TValue>>.Contains (KeyValuePair<TKey, TValue> pair)
                {
-                       return ContainsKey (pair.Key);
+                       TValue value;
+                       if (!TryGetValue (pair.Key, out value))
+                               return false;
+
+                       return EqualityComparer<TValue>.Default.Equals (value, pair.Value);
                }
 
                public KeyValuePair<TKey,TValue>[] ToArray ()
index 698a25588f63abdaca2e1f7553533ca6fc5e512a..e38c904590094c4d205afd198fea736b31ea4bdd 100644 (file)
@@ -353,6 +353,19 @@ namespace MonoTests.System.Collections.Concurrent
                        } catch (ArgumentNullException ex) {
                        }
                }
+               
+               [Test]
+               public void ContainsKeyPairTest ()
+               {
+                       var validKeyPair = new KeyValuePair<string, string> ("key", "validValue");
+                       var wrongKeyPair = new KeyValuePair<string, string> ("key", "wrongValue");
+
+                       IDictionary<string, string> dict = new ConcurrentDictionary<string, string> ();
+                       dict.Add (validKeyPair);
+
+                       Assert.IsTrue (dict.Contains (validKeyPair));
+                       Assert.IsFalse (dict.Contains (wrongKeyPair));
+               }
        }
 }
 #endif