Class IEnumerableExtensions

Namespace
Wolfgang.Extensions.IEnumerable
Assembly
Wolfgang.Extensions.IEnumerable.dll

A collection of extension methods for IEnumerable{T}

public static class IEnumerableExtensions
Inheritance
IEnumerableExtensions
Inherited Members

Methods

Do<T>(IEnumerable<T>?, Action<T>)

Executes a side-effect action on each element of an IEnumerable{T} without transforming the elements. The original items are yielded unchanged.

public static IEnumerable<T> Do<T>(this IEnumerable<T>? source, Action<T> action)

Parameters

source IEnumerable<T>

An IEnumerable{T} whose elements the action will be executed on.

action Action<T>

The action to execute on each element.

Returns

IEnumerable<T>

An IEnumerable{T} that yields the original elements after executing the action.

Type Parameters

T

The type of the elements of source.

Examples

foreach (var item in source.Do(x => Console.WriteLine($"Processing: {x}")))
{
    // item is unchanged
}

Exceptions

ArgumentNullException

source or action is null.

ForEach<T>(IEnumerable<T>?, Action<T>)

Executes the specified action on each item in the enumerable

public static void ForEach<T>(this IEnumerable<T>? source, Action<T> action)

Parameters

source IEnumerable<T>

An IEnumerable{T} whose elements the action will be executed on.

action Action<T>

The action to execute on each item in the enumerable

Type Parameters

T

The type of the elements of source.

Examples

var numbers = new[] { 1, 2, 3, 4, 5 };
numbers.ForEach(n => Console.WriteLine(n));

Exceptions

ArgumentNullException

source or action is null.

IsEmpty<T>(IEnumerable<T>?)

Gets a value indicating whether a sequence contains no elements.

public static bool IsEmpty<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

The IEnumerable{T} to check.

Returns

bool

true if the source sequence contains no elements; otherwise, false.

Type Parameters

T

The type of the elements of source.

Examples

var items = new List<string>();
Console.WriteLine(items.IsEmpty()); // Prints true

Exceptions

ArgumentNullException

source is null.

IsNullOrEmpty<T>(IEnumerable<T>?)

Gets a value indicating whether a sequence is null or contains no elements.

public static bool IsNullOrEmpty<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

The IEnumerable{T} to check.

Returns

bool

true if the source sequence is null or contains no elements; otherwise, false.

Type Parameters

T

The type of the elements of source.

Examples

IEnumerable<string>? data = null;
Console.WriteLine(data.IsNullOrEmpty()); // Prints true

None<T>(IEnumerable<T>?)

Determines whether a sequence contains no elements.

public static bool None<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

The IEnumerable{T} to check.

Returns

bool

false if the source sequence contains any elements; otherwise, true.

Type Parameters

T

The type of the elements of source.

Examples

var numbers = new[] { 1, 2, 3, 4 };
Console.WriteLine(numbers.None()); // Prints false

numbers = new int[] {};
Console.WriteLine(numbers.None()); // Prints true

Exceptions

ArgumentNullException

source is null.

None<T>(IEnumerable<T>?, Func<T, bool>)

Determines whether no element of a sequence satisfies a condition.

public static bool None<T>(this IEnumerable<T>? source, Func<T, bool> predicate)

Parameters

source IEnumerable<T>

An IEnumerable{T} whose elements to apply the predicate to.

predicate Func<T, bool>

A function to test each element for a condition.

Returns

bool

true if no elements in the source sequence pass the test in the specified predicate; otherwise, false.

Type Parameters

T

The type of the elements of source.

Examples

var numbers = new[] { 1, 2, 3, 4 };
Console.WriteLine(numbers.None(n => n % 3 == 0)); // Prints false
Console.WriteLine(numbers.None(n => n % 5 == 0)); // Prints true

Exceptions

ArgumentNullException

source or predicate is null.

Shuffle<T>(IEnumerable<T>?)

Creates a new sequence containing the elements from source in a random order, using a Fisher–Yates shuffle.

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

An IEnumerable{T} whose elements will be randomly ordered.

Returns

IEnumerable<T>

A new sequence containing every element of source in a random permutation. The input sequence is not mutated.

Type Parameters

T

The type of the elements of source.

Examples

var deck = new[] { "Ace", "King", "Queen", "Jack" };
var shuffled = deck.Shuffle();

Remarks

This method is eager: the entire source is consumed and the result is materialized before this method returns. The returned sequence does NOT preserve deferred-execution semantics — call .Shuffle() inside a LINQ pipeline only when that buffering cost is acceptable.

The runtime type of the returned sequence is intentionally opaque — pattern matches such as result is List<T>, result is ICollection<T>, and result is T[] all return false. This prevents callers from mutating the shuffle's internal buffer and matches the type-opacity contract of ToEnumerable<T>(IEnumerable<T>?).

Exceptions

ArgumentNullException

source is null.

ToEnumerable<T>(IEnumerable<T>?)

Wraps an IEnumerable<T> in a lazy iterator so the returned sequence is guaranteed not to be assignable to any more concrete enumerable type. Specifically, pattern-matching checks such as result is List<T>, result is ICollection<T>, and result is T[] all return false. Useful in tests (and rarely in production code) that want to defeat fast paths which pattern-match on the runtime type of the source.

public static IEnumerable<T> ToEnumerable<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

An IEnumerable<T> to wrap.

Returns

IEnumerable<T>

A new IEnumerable<T> that yields the elements of source one at a time without exposing the concrete type of the underlying collection.

Type Parameters

T

The type of the elements of source.

Examples

var list = new List<int> { 1, 2, 3 };
var wrapped = list.ToEnumerable();
// `wrapped is List<int>` is false; `wrapped is ICollection<int>` is false.
// Useful when exercising an extension method's slow path in unit tests.

Exceptions

ArgumentNullException

source is null.