diff --git a/src/System.Collections.Concurrent/tests/System.Collections.Concurrent.Tests.csproj b/src/System.Collections.Concurrent/tests/System.Collections.Concurrent.Tests.csproj
index 88f05125e65c..5d3c9a88962d 100644
--- a/src/System.Collections.Concurrent/tests/System.Collections.Concurrent.Tests.csproj
+++ b/src/System.Collections.Concurrent/tests/System.Collections.Concurrent.Tests.csproj
@@ -10,7 +10,7 @@
-
+
Common\System\Collections\DictionaryExtensions.cs
diff --git a/src/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj b/src/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj
index 01a3daf0d60d..0562168caad7 100644
--- a/src/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj
+++ b/src/System.Collections.Immutable/tests/System.Collections.Immutable.Tests.csproj
@@ -14,7 +14,7 @@
-
+
Common\System\Collections\DictionaryExtensions.cs
diff --git a/src/System.Collections/ref/System.Collections.cs b/src/System.Collections/ref/System.Collections.cs
index 81db6619e614..feeb5b8632dc 100644
--- a/src/System.Collections/ref/System.Collections.cs
+++ b/src/System.Collections/ref/System.Collections.cs
@@ -49,6 +49,9 @@ public static class CollectionExtensions
public static TValue GetValueOrDefault(this IReadOnlyDictionary dictionary, TKey key) { throw null; }
public static TValue GetValueOrDefault(this IReadOnlyDictionary dictionary, TKey key, TValue defaultValue) { throw null; }
+
+ public static bool TryAdd(this IDictionary dictionary, TKey key, TValue value) { throw null; }
+ public static bool Remove(this IDictionary dictionary, TKey key, out TValue value) { throw null; }
}
public abstract partial class Comparer : System.Collections.Generic.IComparer, System.Collections.IComparer
{
diff --git a/src/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs b/src/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs
index e3a5ef2e95a8..4a4078d565c0 100644
--- a/src/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs
+++ b/src/System.Collections/src/System/Collections/Generic/CollectionExtensions.cs
@@ -37,5 +37,38 @@ public static TValue GetValueOrDefault(this IReadOnlyDictionary(this IDictionary dictionary, TKey key, TValue value)
+ {
+ if (dictionary == null)
+ {
+ throw new ArgumentNullException(nameof(dictionary));
+ }
+
+ if (!dictionary.ContainsKey(key))
+ {
+ dictionary.Add(key, value);
+ return true;
+ }
+
+ return false;
+ }
+
+ public static bool Remove(this IDictionary dictionary, TKey key, out TValue value)
+ {
+ if (dictionary == null)
+ {
+ throw new ArgumentNullException(nameof(dictionary));
+ }
+
+ if (dictionary.TryGetValue(key, out value))
+ {
+ dictionary.Remove(key);
+ return true;
+ }
+
+ value = default(TValue);
+ return false;
+ }
}
}
diff --git a/src/System.Collections/tests/Generic/CollectionExtensionsTests.cs b/src/System.Collections/tests/Generic/CollectionExtensionsTests.cs
index 74501974d91a..13d5f7aa56e0 100644
--- a/src/System.Collections/tests/Generic/CollectionExtensionsTests.cs
+++ b/src/System.Collections/tests/Generic/CollectionExtensionsTests.cs
@@ -72,5 +72,70 @@ public void GetValueOrDefault_NullIReadOnlyDictionary_ThrowsArgumentNullExceptio
Assert.Throws("dictionary", () => dictionary.GetValueOrDefault("key"));
Assert.Throws("dictionary", () => dictionary.GetValueOrDefault("key", "value"));
}
+
+ [Fact]
+ public void TryAdd_NullIDictionary_ThrowsArgumentNullException()
+ {
+ IDictionary dictionary = null;
+ Assert.Throws("dictionary", () => dictionary.TryAdd("key", "value"));
+ }
+
+ [Fact]
+ public void TryAdd_NullKeyIDictionary_ThrowsArgumentNullException()
+ {
+ IDictionary dictionary = new SortedDictionary();
+ Assert.Throws("key", () => dictionary.TryAdd(null, "value"));
+ }
+
+ [Fact]
+ public void TryAdd_KeyDoesntExistInIDictionary_ReturnsTrue()
+ {
+ IDictionary dictionary = new SortedDictionary();
+ Assert.True(dictionary.TryAdd("key", "value"));
+ Assert.Equal("value", dictionary["key"]);
+ }
+
+ [Fact]
+ public void TryAdd_KeyExistsInIDictionary_ReturnsFalse()
+ {
+ IDictionary dictionary = new SortedDictionary() { ["key"] = "value" };
+ Assert.False(dictionary.TryAdd("key", "value2"));
+ Assert.Equal("value", dictionary["key"]);
+ }
+
+ [Fact]
+ public void Remove_NullIDictionary_ThrowsArgumentNullException()
+ {
+ IDictionary dictionary = null;
+ string value = null;
+ Assert.Throws("dictionary", () => dictionary.Remove("key", out value));
+ Assert.Null(value);
+ }
+
+ [Fact]
+ public void Remove_NullKeyIDictionary_ThrowsArgumentNullException()
+ {
+ IDictionary dictionary = new SortedDictionary();
+ string value = null;
+ Assert.Throws("key", () => dictionary.Remove(null, out value));
+ Assert.Null(value);
+ }
+
+ [Fact]
+ public void Remove_KeyExistsInIDictionary_ReturnsTrue()
+ {
+ IDictionary dictionary = new SortedDictionary() { ["key"] = "value" };
+ Assert.True(dictionary.Remove("key", out var value));
+ Assert.Equal("value", value);
+ Assert.Throws(() => dictionary["key"]);
+ }
+
+ [Fact]
+ public void Remove_KeyDoesntExistInIDictionary_ReturnsFalse()
+ {
+ IDictionary dictionary = new SortedDictionary();
+ Assert.False(dictionary.Remove("key", out var value));
+ Assert.Equal(default(string), value);
+ }
}
}
diff --git a/src/System.Collections/tests/System.Collections.Tests.csproj b/src/System.Collections/tests/System.Collections.Tests.csproj
index d820abc79a0d..1a3a94b83614 100644
--- a/src/System.Collections/tests/System.Collections.Tests.csproj
+++ b/src/System.Collections/tests/System.Collections.Tests.csproj
@@ -71,7 +71,7 @@
Common\System\ObjectCloner.cs
-
+
Common\System\Collections\DictionaryExtensions.cs
@@ -161,4 +161,4 @@
-
\ No newline at end of file
+