Skip to content

Commit

Permalink
Add support for `Dictionary<TKey, TValue>(TKey key, out TValue value)…
Browse files Browse the repository at this point in the history
… Remove` (#93)
  • Loading branch information
jahav authored Sep 16, 2023
1 parent 9b4838a commit fe51936
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
* `Boolean TryFormat(Span<Char>, Int32&, ReadOnlySpan<Char>, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryformat)


### Dictionary<TKey, TValue>

* `Boolean Remove(TKey, out TValue)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove)


### Double

* `Boolean TryFormat(Span<Char>, Int32&, ReadOnlySpan<Char>, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat)
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ The class `PolyfillExtensions` includes the following extension methods:
* `Boolean TryFormat(Span<Char>, Int32&, ReadOnlySpan<Char>, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryformat)


### Dictionary<TKey, TValue>

* `Boolean Remove(TKey, out TValue)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove)


### Double

* `Boolean TryFormat(Span<Char>, Int32&, ReadOnlySpan<Char>, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat)
Expand Down
35 changes: 35 additions & 0 deletions src/Polyfill/PolyfillExtensions_Dictionary.cs
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
27 changes: 27 additions & 0 deletions src/Tests/PolyfillExtensionsTests_Dictionary.cs
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 _));
}
}

0 comments on commit fe51936

Please sign in to comment.