Enumerable Tools

Enumerable tools to help you manipulate with them!

Enumerable extensions can be found in the EnumerableExtensions static class that is found under the Magico.Enumeration namespace. You don't have to explicitly reference the class, because you can just place a dot after the IEnumerable instance.

You can access the extensions in one of the forms:

// Form 1
int length = enumerableVar.Length();

// Form 2
int length2 = EnumerableExtensions.Length(enumerableVar);

Length()

EnumerableExtensions.cs
public static int Length(this IEnumerable enumerable)

This extension counts the elements inside this enumerable class instance, and it's adaptive to several enumerable types for performance, as in:

  • Arrays

  • Lists

  • Dictionaries

  • Collections

  • Non-generic IEnumerable instances

In case this extension encounters an unknown non-generic IEnumerable instance, it'll attempt to generate a generic IEnumerable<object> class prior to counting the elements.

GetElementFromIndex()

EnumerableExtensions.cs
public static object? GetElementFromIndex(this IEnumerable enumerable, int index)

Gets an element using the index number from the enumerable that is either generic or non-generic. It's adaptive to several enumerable types for performance, as in:

  • Arrays

  • Lists

  • Dictionaries

  • Collections

  • Non-generic IEnumerable instances

In case this extension encounters an unknown non-generic IEnumerable instance, it'll attempt to generate a generic IEnumerable<object> class prior to counting the elements.

Zip()

EnumerableExtensions.cs
public static IEnumerable<(TFirst First, TSecond Second)> Zip<TFirst, TSecond>(
    this IEnumerable<TFirst> source, IEnumerable<TSecond> second)

This extension merges two generic IEnumerable instances into one.

ToDictionarySafe()

EnumerableExtensions.cs
public static Dictionary<TKey, TValue> ToDictionarySafe<TSource, TKey, TValue>(
    this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TValue> valueSelector)
    where TKey : notnull

This extension converts the source enumerable instance of the source type to a dictionary that is of a selected key type and value type. This ensures safe conversion.

Last updated