Skip to content

Enumerable Extensions

JasperFx.Core provides extension methods for working with collections and enumerables.

Usage

csharp
using JasperFx.Core;

Examples

cs
public void EnumerableHelpers()
{
    var items = new List<string> { "a", "b", "c", "a" };

    // AddRange that works on IList
    items.Fill("d");

    // Add only if not already present
    items.Fill("a"); // no-op if already present

    // Each / EachAsync for side effects
    items.Each(item => Console.WriteLine(item));
}

snippet source | anchor

Available Methods

MethodDescription
Fill(item)Adds an item to a list (alias for Add)
FillWithUnique(item)Adds only if the item is not already in the list
Each(action)Executes an action for each item
EachAsync(func)Executes an async function for each item sequentially
AddRange(items)Adds multiple items to an IList<T>
IsEmpty()Returns true if the enumerable has no elements
Top(n)Returns the first N items
As<T>()Casts each element to type T

Fill vs Add

Fill is semantically equivalent to Add but reads better in fluent chains. FillWithUnique is useful for building distinct collections without using HashSet:

csharp
var tags = new List<string>();
tags.FillWithUnique("dotnet");
tags.FillWithUnique("dotnet"); // no duplicate added

Each vs ForEach

Each works on any IEnumerable<T>, not just List<T>:

csharp
IEnumerable<string> items = GetItems();
items.Each(item => Process(item));

Next Steps

Released under the MIT License.