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
sourceIEnumerable<T>The source sequence to batch.
batchSizeintThe maximum number of elements per batch.
Returns
- IEnumerable<IEnumerable<T>>
A sequence of batches, each containing at most
batchSizeelements.
Type Parameters
TThe 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
sourceis null.- ArgumentOutOfRangeException
batchSizeis 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
sourceIEnumerable<T>The source sequence.
keySelectorFunc<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
TThe type of elements in the source.
TKeyThe 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
sourceorkeySelectoris 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
sourceIEnumerable<T>The source sequence.
defaultValueTThe value to return if the sequence is empty.
Returns
- T
The first element if the sequence is non-empty; otherwise,
defaultValue.
Type Parameters
TThe 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
sourceis 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
sourceIEnumerable<T>The source sequence to iterate.
actionAction<T, int>The action to execute on each element with its zero-based index.
Type Parameters
TThe type of elements in the source.
Examples
var names = new[] { "Alice", "Bob" };
names.ForEach((name, index) => Console.WriteLine($"{index}: {name}"));
Exceptions
- ArgumentNullException
sourceoractionis 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
sourceIEnumerable<T>The source sequence to iterate.
actionAction<T>The action to execute on each element.
Type Parameters
TThe type of elements in the source.
Examples
var names = new[] { "Alice", "Bob" };
names.ForEach(name => Console.WriteLine(name));
Exceptions
- ArgumentNullException
sourceoractionis 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
dictionaryIDictionary<TKey, T>The dictionary to search.
keyTKeyThe key to look up.
defaultValueTThe value to return if the key is not found.
Returns
- T
The value for the key if found; otherwise,
defaultValue.
Type Parameters
TKeyThe type of keys in the dictionary.
TThe 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
dictionaryis 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
collectionIEnumerable<T>The collection to check.
Returns
- bool
trueifcollectionis notnulland contains at least one element; otherwise,false.
Type Parameters
TThe 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
collectionIEnumerable<T>The collection to check.
Returns
- bool
trueifcollectionisnullor empty; otherwise,false.
Type Parameters
TThe 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
listIReadOnlyList<T>The list to return, or
null.
Returns
- IReadOnlyList<T>
The original
listif non-null; otherwise, an empty IReadOnlyList<T>.
Type Parameters
TThe 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
collectionIEnumerable<T>The collection to return, or
null.
Returns
- IEnumerable<T>
The original
collectionif non-null; otherwise, an empty IEnumerable<T>.
Type Parameters
TThe type of elements in the collection.
Examples
IEnumerable<int>? numbers = null;
foreach (var n in numbers.OrEmpty())
{
// safely iterates — no NullReferenceException
}