-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for `Dictionary<TKey, TValue>(TKey key, out TValue value)…
… Remove` (#93)
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// <auto-generated /> | ||
|
||
#pragma warning disable | ||
|
||
#if NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2X | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Link = System.ComponentModel.DescriptionAttribute; | ||
|
||
static partial class PolyfillExtensions | ||
{ | ||
/// <summary> | ||
/// Removes the value with the specified key from the <see cref="Dictionary{TKey,TValue}"/>, and copies the element | ||
/// to the value parameter. | ||
/// </summary> | ||
/// <param name="target">A dictionary with keys of type TKey and values of type TValue.</param> | ||
/// <param name="key">The key of the element to remove.</param> | ||
/// <param name="value">The removed element.</param> | ||
/// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam> | ||
/// <typeparam name="TValue">The type of the values in the dictionary.</typeparam> | ||
/// <returns><code>true</code> if the element is successfully found and removed; otherwise, <code>false</code>.</returns> | ||
/// <exception cref="ArgumentNullException"><paramref name="key"/> is <code>null</code>.</exception> | ||
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove")] | ||
public static bool Remove<TKey, TValue>( | ||
this Dictionary<TKey,TValue> target, | ||
TKey key, | ||
[MaybeNullWhen(false)] out TValue value) | ||
{ | ||
target.TryGetValue(key, out value); | ||
return target.Remove(key); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
partial class PolyfillExtensionsTests | ||
{ | ||
[Test] | ||
public void Dictionary_Remove() | ||
{ | ||
var dictionary = new Dictionary<string, string?> { {"key", "value"} }; | ||
|
||
Assert.True(dictionary.Remove("key", out var value)); | ||
Assert.AreEqual("value", value); | ||
} | ||
|
||
[Test] | ||
public void Dictionary_Remove_DoesntThrowOnMissingKey() | ||
{ | ||
var dictionary = new Dictionary<string, string?>(); | ||
|
||
Assert.False(dictionary.Remove("non-existent key", out var value)); | ||
Assert.AreEqual(default, value); | ||
} | ||
|
||
[Test] | ||
public void Dictionary_Remove_ThrowsOnNull() | ||
{ | ||
var dictionary = new Dictionary<string, string>(); | ||
Assert.Throws<ArgumentNullException>(() => dictionary.Remove(null!, out _)); | ||
} | ||
} |