EnumerableExtensions Class

Provides extension methods for types implementing IEnumerable or IEnumerable<T>.

public static class EnumerableExtensions

Methods

NameDescription
IEnumerable.DisposeAll()Disposes all items that implement either IDisposable or IAsyncDisposable. If an item implements both, only IDisposable is considered.
IEnumerable.DisposeAllAsync()Asynchronously disposes all items that implement either IAsyncDisposable or IDisposable. If an item implements both, only IAsyncDisposable is considered.
IEnumerable<T>.WhereNot(Func<T, bool>)Filters a sequence of values based on a predicate, taking only those that do not satisfy the condition set by the predicate.
WhereNotNull<T>(IEnumerable<T?>)Filters a sequence of nullable values, taking only those that are not null.
WhereNotNull<T>(IEnumerable<T?>)Filters a sequence of nullable values, taking only those that are not null.
IEnumerable<string?>.WhereNotNullOrEmpty()Filters a sequence of nullable strings, taking only those that are neither null nor the empty string.
IEnumerable<string?>.WhereNotNullOrWhiteSpace()Filters a sequence of nullable strings, taking only those that are not null, nor empty, nor consisting only of white-space characters.

IEnumerable.DisposeAll()

Disposes all items that implement either IDisposable or IAsyncDisposable.

If an item implements both, only IDisposable is considered.

extension(IEnumerable @this)
{
    public void DisposeAll()
}

Parameters

this IEnumerable
The IEnumerable interface on which this method is called.

Exceptions

ArgumentNullException
this is null.
AggregateException
One or more exceptions were raised by the IDisposable.Dispose and/or IAsyncDisposable.DisposeAsync methods of items.

Remarks

This method will not dispose disposable ref structs.

IEnumerable.DisposeAllAsync()

Asynchronously disposes all items that implement either IAsyncDisposable or IDisposable.

If an item implements both, only IAsyncDisposable is considered.

extension(IEnumerable @this)
{
    public ValueTask DisposeAllAsync()
}

Parameters

this IEnumerable
The IEnumerable interface on which this method is called.

Returns

ValueTask A ValueTask that represents the ongoing operation.

Exceptions

ArgumentNullException
this is null.
AggregateException
One or more exceptions were raised by the IAsyncDisposable.DisposeAsync and/or IDisposable.Dispose methods of items.

Remarks

This method will not dispose disposable ref structs.

IEnumerable<T>.WhereNot(Func<T, bool>)

Filters a sequence of values based on a predicate, taking only those that do not satisfy the condition set by the predicate.

extension<T>(IEnumerable<T> @this)
{
    public IEnumerable<T> WhereNot(Func<T, bool> predicate)
}

Type parameters

T
The type of the elements of this.

Parameters

this IEnumerable<T>
An IEnumerable<T> to filter.
predicate Func<T, bool>
A function to test each element for a condition.

Returns

IEnumerable<T> An IEnumerable<T> that contains elements from the input sequence that do not satisfy the condition defined by predicate.

Exceptions

ArgumentNullException
this is null. - or - predicate is null.

WhereNotNull overloads

WhereNotNull<T>(IEnumerable<T?>)

Filters a sequence of nullable values, taking only those that are not null.

public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> @this) where T : struct
Type parameters
T
The type of the elements of this.
Parameters
this IEnumerable<T?>
An IEnumerable<T> to filter.
Returns

IEnumerable<T> An IEnumerable<T> that contains the values of the elements from the input sequence that are not null.

Exceptions
ArgumentNullException
this is null.

WhereNotNull<T>(IEnumerable<T?>)

Filters a sequence of nullable values, taking only those that are not null.

public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T?> @this) where T : class
Type parameters
T
The type of the elements of this.
Parameters
this IEnumerable<T?>
An IEnumerable<T> to filter.
Returns

IEnumerable<T> An IEnumerable<T> that contains elements from the input sequence that are not null.

Exceptions
ArgumentNullException
this is null.

IEnumerable<string?>.WhereNotNullOrEmpty()

Filters a sequence of nullable strings, taking only those that are neither null nor the empty string.

extension(IEnumerable<string?> @this)
{
    public IEnumerable<string> WhereNotNullOrEmpty()
}

Parameters

this IEnumerable<string?>
An IEnumerable<string?> to filter.

Returns

IEnumerable<string> An IEnumerable<string> that contains elements from the input sequence that are neither null nor the empty string.

Exceptions

ArgumentNullException
this is null.

IEnumerable<string?>.WhereNotNullOrWhiteSpace()

Filters a sequence of nullable strings, taking only those that are not null, nor empty, nor consisting only of white-space characters.

extension(IEnumerable<string?> @this)
{
    public IEnumerable<string> WhereNotNullOrWhiteSpace()
}

Parameters

this IEnumerable<string?>
An IEnumerable<string?> to filter.

Returns

IEnumerable<string> An IEnumerable<string> that contains elements from the input sequence that are not null, nor empty, nor consisting only of white-space characters.

Exceptions

ArgumentNullException
this is null.