DotNext by Roman Sakno

<PackageReference Include="DotNext" Version="0.11.0" />

 Sequence

public static class Sequence
Various methods to work with classes implementing IEnumerable<T> interface.
using DotNext.Collections.Generic; using System; using System.Collections.Generic; using System.Linq; namespace DotNext { public static class Sequence { public static int SequenceHashCode(this IEnumerable<object> sequence, bool salted = true) { int num = sequence.Aggregate(-910176598, (int current, object item) => current * -1521134295 + (item?.GetHashCode()).GetValueOrDefault()); if (!salted) return num; return num * -1521134295 + RandomExtensions.BitwiseHashSalt; } public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action) { foreach (T item in collection) { action(item); } } public static T? FirstOrNull<T>(this IEnumerable<T> seq) where T : struct { using (IEnumerator<T> enumerator = seq.GetEnumerator()) return enumerator.MoveNext() ? new T?(enumerator.Current) : null; } public static Optional<T> FirstOrEmpty<T>(this IEnumerable<T> seq) { using (IEnumerator<T> enumerator = seq.GetEnumerator()) return enumerator.MoveNext() ? ((Optional<T>)enumerator.Current) : Optional<T>.Empty; } public static bool Skip<T>(this IEnumerator<T> enumerator, int count) { while (count > 0) { if (!enumerator.MoveNext()) return false; count--; } return true; } private static bool ElementAt<T>(this IList<T> list, int index, out T element) { if (index >= 0 && index < ((ICollection<T>)list).Count) { element = list[index]; return true; } element = default(T); return false; } private static bool ElementAt<T>(this IReadOnlyList<T> list, int index, out T element) { if (index >= 0 && index < ((IReadOnlyCollection<T>)list).Count) { element = list[index]; return true; } element = default(T); return false; } public static bool ElementAt<T>(this IEnumerable<T> collection, int index, out T element) { IList<T> list = collection as IList<T>; if (list != null) return list.ElementAt(index, out element); IReadOnlyList<T> readOnlyList = collection as IReadOnlyList<T>; if (readOnlyList != null) return readOnlyList.ElementAt(index, out element); using (IEnumerator<T> enumerator = collection.GetEnumerator()) { enumerator.Skip(index); if (!enumerator.MoveNext()) { element = default(T); return false; } element = enumerator.Current; return true; } } public static IEnumerable<T> SkipNulls<T>(this IEnumerable<T> collection) where T : class { return collection.Where(Func.IsNotNull<T>()); } public static string ToString<T>(this IEnumerable<T> collection, string delimiter, string ifEmpty = "") { return string.Join(delimiter, collection).IfNullOrEmpty(ifEmpty); } public static IEnumerable<T> Singleton<T>(T item) { return List.Singleton(item); } public static IEnumerable<T> Prepend<T>(this IEnumerable<T> collection, params T[] items) { return items.Concat(collection); } public static IEnumerable<T> Append<T>(this IEnumerable<T> collection, params T[] items) { return collection.Concat(items); } } }