3 changed files with 66 additions and 53 deletions
@ -1,32 +1,44 @@ |
|||||
using System.Collections.Generic; |
using System.Collections.Generic; |
||||
using System.Diagnostics.CodeAnalysis; |
using System.Diagnostics.CodeAnalysis; |
||||
|
using System.Runtime.CompilerServices; |
||||
namespace System; |
|
||||
|
|
||||
#if !NET6_0_OR_GREATER
|
#if !NET6_0_OR_GREATER
|
||||
internal static class CollectionCompatibilityExtensions |
namespace System |
||||
{ |
{ |
||||
public static bool Remove<TKey, TValue>( |
internal static class CollectionCompatibilityExtensions |
||||
this Dictionary<TKey, TValue> o, |
|
||||
TKey key, |
|
||||
[MaybeNullWhen(false)] out TValue value) |
|
||||
where TKey : notnull |
|
||||
{ |
{ |
||||
if (o.TryGetValue(key, out value)) |
public static bool Remove<TKey, TValue>( |
||||
return o.Remove(key); |
this Dictionary<TKey, TValue> o, |
||||
return false; |
TKey key, |
||||
} |
[MaybeNullWhen(false)] out TValue value) |
||||
|
where TKey : notnull |
||||
|
{ |
||||
|
if (o.TryGetValue(key, out value)) |
||||
|
return o.Remove(key); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> o, TKey key, TValue value) |
public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> o, TKey key, TValue value) |
||||
where TKey : notnull |
where TKey : notnull |
||||
|
{ |
||||
|
if (!o.ContainsKey(key)) |
||||
|
{ |
||||
|
o.Add(key, value); |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
namespace Runtime.InteropServices |
||||
{ |
{ |
||||
if (!o.ContainsKey(key)) |
public static class CollectionsMarshal |
||||
{ |
{ |
||||
o.Add(key, value); |
public static Span<T> AsSpan<T>(List<T>? list) |
||||
return true; |
=> list is null ? default : new Span<T>(Unsafe.As<StrongBox<T[]>>(list).Value, 0, list.Count); |
||||
} |
} |
||||
|
|
||||
return false; |
|
||||
} |
} |
||||
} |
} |
||||
|
|
||||
#endif
|
#endif
|
||||
|
|||||
Loading…
Reference in new issue