Class CollectionExtensions
- Namespace
- LfrlAnvil.Extensions
- Assembly
- LfrlAnvil.Core.dll
Contains extension methods for materialized collections.
public static class CollectionExtensions
- Inheritance
-
CollectionExtensions
- Inherited Members
Methods
ContainsAtLeast<T>(IReadOnlyCollection<T>, int)
Checks if the provided source contains at least count number of elements.
[Pure]
public static bool ContainsAtLeast<T>(this IReadOnlyCollection<T> source, int count)
Parameters
sourceIReadOnlyCollection<T>Source collection.
countintExpected minimum number of elements.
Returns
- bool
true when
sourcecontains correct number of elements, otherwise false.
Type Parameters
TCollection element type.
ContainsAtMost<T>(IReadOnlyCollection<T>, int)
Checks if the provided source contains at most count number of elements.
[Pure]
public static bool ContainsAtMost<T>(this IReadOnlyCollection<T> source, int count)
Parameters
sourceIReadOnlyCollection<T>Source collection.
countintExpected maximum number of elements.
Returns
- bool
true when
sourcecontains correct number of elements, otherwise false.
Type Parameters
TCollection element type.
ContainsExactly<T>(IReadOnlyCollection<T>, int)
Checks if the provided source contains exactly count number of elements.
[Pure]
public static bool ContainsExactly<T>(this IReadOnlyCollection<T> source, int count)
Parameters
sourceIReadOnlyCollection<T>Source collection.
countintExpected exact number of elements.
Returns
- bool
true when
sourcecontains correct number of elements, otherwise false.
Type Parameters
TCollection element type.
ContainsInRange<T>(IReadOnlyCollection<T>, int, int)
Checks if the provided source contains between
minCount and maxCount number of elements.
[Pure]
public static bool ContainsInRange<T>(this IReadOnlyCollection<T> source, int minCount, int maxCount)
Parameters
sourceIReadOnlyCollection<T>Source collection.
minCountintExpected minimum number of elements.
maxCountintExpected maximum number of elements.
Returns
- bool
true when
sourcecontains correct number of elements, otherwise false.
Type Parameters
TCollection element type.
CopyTo<T>(IReadOnlyCollection<T>, RentedMemorySequenceSpan<T>)
Copies the provided source into the given span.
public static void CopyTo<T>(this IReadOnlyCollection<T> source, RentedMemorySequenceSpan<T> span)
Parameters
sourceIReadOnlyCollection<T>Source collection.
spanRentedMemorySequenceSpan<T>Target RentedMemorySequenceSpan<T> to copy
sourceelements to.
Type Parameters
TCollection element type.
Remarks
Copying starts at the beginning of the span.
Exceptions
- ArgumentException
When
sourcecontains more elements than thespancan hold.
EmptyIfNull<T>(IReadOnlyCollection<T>?)
Returns the provided source, unless it is null, in which case returns an empty array instead.
[Pure]
public static IReadOnlyCollection<T> EmptyIfNull<T>(this IReadOnlyCollection<T>? source)
Parameters
sourceIReadOnlyCollection<T>Source collection.
Returns
- IReadOnlyCollection<T>
sourceif it is not null, otherwise an empty array.
Type Parameters
TCollection element type.
GetOrAddAsync<TKey, TValue>(ICache<TKey, TValue>, TKey, Func<TKey, CancellationToken, Task<TValue>>, CancellationToken)
Reads a value associated with the specified key from the cache, if it exists.
Otherwise adds a new entry to the cache with the value returned by the asynchronous provider
invocation and returns that value.
public static ValueTask<TValue> GetOrAddAsync<TKey, TValue>(this ICache<TKey, TValue> cache, TKey key, Func<TKey, CancellationToken, Task<TValue>> provider, CancellationToken cancellationToken = default) where TKey : notnull
Parameters
cacheICache<TKey, TValue>Source cache.
keyTKeyEntry's key.
providerFunc<TKey, CancellationToken, Task<TValue>>Entry's asynchronous value provider. Gets invoked only when entry with the provided
keydoes not exist in thecache.cancellationTokenCancellationTokenOptional CancellationToken forwarded to the
provider.
Returns
- ValueTask<TValue>
Value associated with the provided
key, if it exists, otherwise value returned byprovider.
Type Parameters
TKeyCache key type.
TValueCache value type.
GetOrAddAsync<TKey, TValue>(ICache<TKey, TValue>, TKey, Func<TKey, CancellationToken, ValueTask<TValue>>, CancellationToken)
Reads a value associated with the specified key from the cache, if it exists.
Otherwise adds a new entry to the cache with the value returned by the asynchronous provider
invocation and returns that value.
public static ValueTask<TValue> GetOrAddAsync<TKey, TValue>(this ICache<TKey, TValue> cache, TKey key, Func<TKey, CancellationToken, ValueTask<TValue>> provider, CancellationToken cancellationToken = default) where TKey : notnull
Parameters
cacheICache<TKey, TValue>Source cache.
keyTKeyEntry's key.
providerFunc<TKey, CancellationToken, ValueTask<TValue>>Entry's asynchronous value provider. Gets invoked only when entry with the provided
keydoes not exist in thecache.cancellationTokenCancellationTokenOptional CancellationToken forwarded to the
provider.
Returns
- ValueTask<TValue>
Value associated with the provided
key, if it exists, otherwise value returned byprovider.
Type Parameters
TKeyCache key type.
TValueCache value type.
GetOrAdd<TKey, TValue>(ICache<TKey, TValue>, TKey, Func<TKey, TValue>)
Reads a value associated with the specified key from the cache, if it exists.
Otherwise adds a new entry to the cache with the value returned by the provider
invocation and returns that value.
public static TValue GetOrAdd<TKey, TValue>(this ICache<TKey, TValue> cache, TKey key, Func<TKey, TValue> provider) where TKey : notnull
Parameters
cacheICache<TKey, TValue>Source cache.
keyTKeyEntry's key.
providerFunc<TKey, TValue>Entry's value provider. Gets invoked only when entry with the provided
keydoes not exist in thecache.
Returns
- TValue
Value associated with the provided
key, if it exists, otherwise value returned byprovider.
Type Parameters
TKeyCache key type.
TValueCache value type.
IsEmpty<T>(IReadOnlyCollection<T>)
Checks if the provided source is empty.
[Pure]
public static bool IsEmpty<T>(this IReadOnlyCollection<T> source)
Parameters
sourceIReadOnlyCollection<T>Source collection.
Returns
- bool
true when
sourceis empty, otherwise false.
Type Parameters
TCollection element type.
IsNullOrEmpty<T>(IReadOnlyCollection<T>?)
Checks if the provided source is null or empty.
[Pure]
public static bool IsNullOrEmpty<T>(this IReadOnlyCollection<T>? source)
Parameters
sourceIReadOnlyCollection<T>Source collection.
Returns
- bool
true when
sourceis null or empty, otherwise false.
Type Parameters
TCollection element type.