Skip to content

Commit

Permalink
Add ConcurrentBag Clear (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Nov 7, 2024
1 parent ad86a8c commit f8556dd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion apiCount.include.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**API count: 400**
**API count: 401**
5 changes: 5 additions & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
* `Task CancelAsync()` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource.cancelasync)


#### ConcurrentBag<T>

* `void Clear<T>(ConcurrentBag<T>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentbag-1.clear)


#### ConcurrentDictionary<TKey, TValue>

* `TValue GetOrAdd<TKey, TValue, TArg>(TKey, Func<TKey, TArg, TValue>, TArg) where TKey : notnull` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentdictionary-2.getoradd#system-collections-concurrent-concurrentdictionary-2-getoradd-1(-0-system-func((-0-0-1))-0))
Expand Down
7 changes: 7 additions & 0 deletions src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,19 @@ void Type_GetMethod()
type.GetMethod("GenericMethod", 1, BindingFlags.Public, [typeof(string)]);
}
#endif

void ConcurrentDictionary_Methods()
{
var dict = new ConcurrentDictionary<string, int>();
var value = dict.GetOrAdd("Hello", static (_, arg) => arg.Length, "World");
}

void ConcurrentBag_Methods()
{
var bag = new ConcurrentBag<string>();
bag.Clear();
}

void Dictionary_Methods()
{
var dictionary = new Dictionary<string, string?> { { "key", "value" } };
Expand Down
31 changes: 31 additions & 0 deletions src/Polyfill/Polyfill_ConcurrentBag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <auto-generated />
#pragma warning disable

#if NETFRAMEWORK || NETSTANDARD2_0

namespace Polyfills;

using System;
using System.Collections.Concurrent;
using Link = System.ComponentModel.DescriptionAttribute;

static partial class Polyfill
{
/// <summary>
/// Removes all values from the <see cref="ConcurrentBag{T}"/>.
/// </summary>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentbag-1.clear")]
public static void Clear<T>(this ConcurrentBag<T> target)
{
if (target is null)
{
throw new ArgumentNullException(nameof(target));
}

while (!target.IsEmpty)
{
target.TryTake(out _);
}
}
}
#endif
13 changes: 13 additions & 0 deletions src/Tests/PolyfillTests_ConcurrentBag.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
partial class PolyfillTests
{
[Test]
public void ConcurrentBagClear()
{
var bag = new ConcurrentBag<string>
{
"Hello"
};
bag.Clear();
Assert.AreEqual(0, bag.Count);
}
}
2 changes: 1 addition & 1 deletion src/Tests/PolyfillTests_IEnumerable_MinMax.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
partial class PolyfillTests
{
static readonly ReverseComparer reverseComparer = new ReverseComparer();
static readonly ReverseComparer reverseComparer = new();

static readonly IEnumerable<int> comparableValues = [1, 2];
static readonly IEnumerable<int?> comparableNullables = [null, 1, 2];
Expand Down

0 comments on commit f8556dd

Please sign in to comment.