Table of Contents

Class CollectionExtensions

Namespace
DotNetExtensionKit
Assembly
DotNetExtensionKit.dll

Provides extension methods for collections and LINQ helpers including null/empty checks, batching, iteration, and safe element access.

public static class CollectionExtensions
Inheritance
CollectionExtensions
Inherited Members

Methods

Batch<T>(IEnumerable<T>, int)

Splits the source sequence into batches of at most batchSize elements. Uses deferred execution; the source is enumerated lazily.

public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> source, int batchSize)

Parameters

source IEnumerable<T>

The source sequence to batch.

batchSize int

The maximum number of elements per batch.

Returns

IEnumerable<IEnumerable<T>>

A sequence of batches, each containing at most batchSize elements.

Type Parameters

T

The type of elements in the source.

Examples

var numbers = new[] { 1, 2, 3, 4, 5 };
var batches = numbers.Batch(2);
// batches: { {1, 2}, {3, 4}, {5} }

Exceptions

ArgumentNullException

source is null.

ArgumentOutOfRangeException

batchSize is zero or negative.

DistinctBy<T, TKey>(IEnumerable<T>, Func<T, TKey>)

Returns distinct elements from the source sequence based on a key selector, keeping the first occurrence of each key.

public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> source, Func<T, TKey> keySelector)

Parameters

source IEnumerable<T>

The source sequence.

keySelector Func<T, TKey>

A function to extract the key from each element.

Returns

IEnumerable<T>

A sequence of elements with unique keys, preserving the first occurrence.

Type Parameters

T

The type of elements in the source.

TKey

The type of the key used for distinct comparison.

Examples

var people = new[] { ("Alice", 30), ("Bob", 25), ("Alice", 28) };
var distinct = people.DistinctBy(p => p.Item1);
// distinct: { ("Alice", 30), ("Bob", 25) }

Exceptions

ArgumentNullException

source or keySelector is null.

FirstOrDefault<T>(IEnumerable<T>, T)

Returns the first element of the sequence, or the specified default value if the sequence is empty.

public static T FirstOrDefault<T>(this IEnumerable<T> source, T defaultValue)

Parameters

source IEnumerable<T>

The source sequence.

defaultValue T

The value to return if the sequence is empty.

Returns

T

The first element if the sequence is non-empty; otherwise, defaultValue.

Type Parameters

T

The type of elements in the source.

Examples

var empty = Enumerable.Empty<int>();
int result = empty.FirstOrDefault(-1); // -1

var numbers = new[] { 10, 20 };
result = numbers.FirstOrDefault(-1); // 10

Exceptions

ArgumentNullException

source is null.

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

Executes the specified action on each element of the source sequence, providing the zero-based index.

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

Parameters

source IEnumerable<T>

The source sequence to iterate.

action Action<T, int>

The action to execute on each element with its zero-based index.

Type Parameters

T

The type of elements in the source.

Examples

var names = new[] { "Alice", "Bob" };
names.ForEach((name, index) => Console.WriteLine($"{index}: {name}"));

Exceptions

ArgumentNullException

source or action is null.

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

Executes the specified action on each element of the source sequence.

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

Parameters

source IEnumerable<T>

The source sequence to iterate.

action Action<T>

The action to execute on each element.

Type Parameters

T

The type of elements in the source.

Examples

var names = new[] { "Alice", "Bob" };
names.ForEach(name => Console.WriteLine(name));

Exceptions

ArgumentNullException

source or action is null.

GetValueOrDefault<TKey, T>(IDictionary<TKey, T>, TKey, T)

Returns the value associated with the specified key, or the specified default value if the key is not found.

public static T GetValueOrDefault<TKey, T>(this IDictionary<TKey, T> dictionary, TKey key, T defaultValue = default)

Parameters

dictionary IDictionary<TKey, T>

The dictionary to search.

key TKey

The key to look up.

defaultValue T

The value to return if the key is not found.

Returns

T

The value for the key if found; otherwise, defaultValue.

Type Parameters

TKey

The type of keys in the dictionary.

T

The type of values in the dictionary.

Examples

var map = new Dictionary<string, int> { ["a"] = 1 };
int val = map.GetValueOrDefault("b", 42); // 42
val = map.GetValueOrDefault("a", 42); // 1

Exceptions

ArgumentNullException

dictionary is null.

HasItems<T>(IEnumerable<T>?)

Returns true if the collection is not null and contains at least one element.

public static bool HasItems<T>(this IEnumerable<T>? collection)

Parameters

collection IEnumerable<T>

The collection to check.

Returns

bool

true if collection is not null and contains at least one element; otherwise, false.

Type Parameters

T

The type of elements in the collection.

Examples

var names = new List<string> { "Alice" };
bool result = names.HasItems(); // true

List<string>? empty = null;
result = empty.HasItems(); // false

IsNullOrEmpty<T>(IEnumerable<T>?)

Returns true if the collection is null or contains no elements.

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

Parameters

collection IEnumerable<T>

The collection to check.

Returns

bool

true if collection is null or empty; otherwise, false.

Type Parameters

T

The type of elements in the collection.

Examples

List<int>? items = null;
bool result = items.IsNullOrEmpty(); // true

items = new List<int> { 1, 2 };
result = items.IsNullOrEmpty(); // false

OrEmptyList<T>(IReadOnlyList<T>?)

Returns the list if non-null, or an empty read-only list otherwise.

public static IReadOnlyList<T> OrEmptyList<T>(this IReadOnlyList<T>? list)

Parameters

list IReadOnlyList<T>

The list to return, or null.

Returns

IReadOnlyList<T>

The original list if non-null; otherwise, an empty IReadOnlyList<T>.

Type Parameters

T

The type of elements in the list.

Examples

IReadOnlyList<string>? names = null;
IReadOnlyList<string> safe = names.OrEmptyList();
int count = safe.Count; // 0

OrEmpty<T>(IEnumerable<T>?)

Returns the collection if non-null, or an empty enumerable otherwise.

public static IEnumerable<T> OrEmpty<T>(this IEnumerable<T>? collection)

Parameters

collection IEnumerable<T>

The collection to return, or null.

Returns

IEnumerable<T>

The original collection if non-null; otherwise, an empty IEnumerable<T>.

Type Parameters

T

The type of elements in the collection.

Examples

IEnumerable<int>? numbers = null;
foreach (var n in numbers.OrEmpty())
{
    // safely iterates — no NullReferenceException
}