Show / Hide Table of Contents

Class ListExtensions

Inheritance
System.Object
ListExtensions
Namespace: Eco.Shared.Utils
Assembly: Eco.Shared.dll
Syntax
public static class ListExtensions : Object

Methods

AddNonNull<T>(ISet<T>, T)

Add to the hashset if it's not null.

Declaration
public static void AddNonNull<T>(this ISet<T> set, T val)
Parameters
Type Name Description
System.Collections.Generic.ISet<T> set
T val
Type Parameters
Name Description
T

AddNonNullKey<TK, TV>(IDictionary<TK, TV>, TK, TV)

Add to the dictionary if the key is not null.

Declaration
public static void AddNonNullKey<TK, TV>(this IDictionary<TK, TV> dict, TK key, TV val)
Parameters
Type Name Description
System.Collections.Generic.IDictionary<TK, TV> dict
TK key
TV val
Type Parameters
Name Description
TK
TV

AddOnce<T>(IList<T>, T)

Declaration
public static bool AddOnce<T>(this IList<T> list, T entry)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
T entry
Returns
Type Description
System.Boolean
Type Parameters
Name Description
T

AddOrReplace<T>(IList<T>, T, Predicate<T>)

Replaces first element matching the matcher with item or adds new item if no one matched.

Declaration
public static void AddOrReplace<T>(this IList<T> list, T item, Predicate<T> matcher)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
T item
System.Predicate<T> matcher
Type Parameters
Name Description
T

AddOrUpdate<T>(IList<T>, Func<T, Boolean>, Func<T>, Action<T>)

Declaration
public static void AddOrUpdate<T>(this IList<T> list, Func<T, bool> match, Func<T> create, Action<T> update)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Func<T, System.Boolean> match
System.Func<T> create
System.Action<T> update
Type Parameters
Name Description
T

AddUniqueRange<T>(IList<T>, IEnumerable<T>)

Declaration
public static bool AddUniqueRange<T>(this IList<T> list, IEnumerable<T> items)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Collections.Generic.IEnumerable<T> items
Returns
Type Description
System.Boolean
Type Parameters
Name Description
T

AllSame<T>(IEnumerable<T>)

Declaration
public static (bool, T) AllSame<T>(this IEnumerable<T> set)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> set
Returns
Type Description
System.ValueTuple<System.Boolean, T>
Type Parameters
Name Description
T

Clone<T>(IList<T>)

Declaration
public static IList<T> Clone<T>(this IList<T> listToClone)
    where T : ICloneable
Parameters
Type Name Description
System.Collections.Generic.IList<T> listToClone
Returns
Type Description
System.Collections.Generic.IList<T>
Type Parameters
Name Description
T

Count(IEnumerable)

Declaration
public static int Count(this IEnumerable source)
Parameters
Type Name Description
System.Collections.IEnumerable source
Returns
Type Description
System.Int32

EnsureMinLength<T>(IEnumerable<T>, Int32)

Declaration
public static IEnumerable<T> EnsureMinLength<T>(this IEnumerable<T> list, int len)
    where T : struct, ValueType
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> list
System.Int32 len
Returns
Type Description
System.Collections.Generic.IEnumerable<T>
Type Parameters
Name Description
T

Enumerate<T>(BSONArray)

Declaration
public static IEnumerable<T> Enumerate<T>(this BSONArray bsonArray)
Parameters
Type Name Description
BSONArray bsonArray
Returns
Type Description
System.Collections.Generic.IEnumerable<T>
Type Parameters
Name Description
T

EveryNthElement<T>(IEnumerable<T>, Int32)

Declaration
public static IEnumerable<T> EveryNthElement<T>(this IEnumerable<T> list, int n)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> list
System.Int32 n
Returns
Type Description
System.Collections.Generic.IEnumerable<T>
Type Parameters
Name Description
T

FindNearestValue<T>(IList<T>, T, Func<T, T, Single>)

Finds nearest value in collection

Declaration
public static T FindNearestValue<T>(this IList<T> array, T value, Func<T, T, float> distanceFunc)
Parameters
Type Name Description
System.Collections.Generic.IList<T> array
T value
System.Func<T, T, System.Single> distanceFunc
Returns
Type Description
T
Type Parameters
Name Description
T

FindNearestValueAndDistance<T>(IList<T>, T, Func<T, T, Single>)

Finds nearest value and distance in collection

Declaration
public static (T, float) FindNearestValueAndDistance<T>(this IList<T> array, T value, Func<T, T, float> distanceFunc)
Parameters
Type Name Description
System.Collections.Generic.IList<T> array
T value
System.Func<T, T, System.Single> distanceFunc
Returns
Type Description
System.ValueTuple<T, System.Single>
Type Parameters
Name Description
T

FindNearestValueIndex<T>(IList<T>, T, Func<T, T, Single>)

Finds nearest value index in collection

Declaration
public static int FindNearestValueIndex<T>(this IList<T> array, T value, Func<T, T, float> distanceFunc)
Parameters
Type Name Description
System.Collections.Generic.IList<T> array
T value
System.Func<T, T, System.Single> distanceFunc
Returns
Type Description
System.Int32
Type Parameters
Name Description
T

FindNearestValueIndexAndDistance<T>(IList<T>, T, Func<T, T, Single>)

Finds nearest value index and distance in collection

Declaration
public static (int, float) FindNearestValueIndexAndDistance<T>(this IList<T> array, T value, Func<T, T, float> distanceFunc)
Parameters
Type Name Description
System.Collections.Generic.IList<T> array
T value
System.Func<T, T, System.Single> distanceFunc
Returns
Type Description
System.ValueTuple<System.Int32, System.Single>
Type Parameters
Name Description
T

FirstIndex<T>(IList<T>, Predicate<T>)

Declaration
public static int FirstIndex<T>(this IList<T> list, Predicate<T> func)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Predicate<T> func
Returns
Type Description
System.Int32
Type Parameters
Name Description
T

FirstOfTypeOrDefault<TElement, T>(List<TElement>)

Returns first item of type T or default value if not found. It is a list specific implementation to avoid GC-allocations.

Declaration
public static T FirstOfTypeOrDefault<TElement, T>(this List<TElement> list)
    where T : TElement
Parameters
Type Name Description
System.Collections.Generic.List<TElement> list
Returns
Type Description
T
Type Parameters
Name Description
TElement
T

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

Declaration
public static void ForEach<T>(this IList<T> list, Action<T> action)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Action<T> action
Type Parameters
Name Description
T

ForEachIndex<T>(IList<T>, Action<T, Int32>)

Declaration
public static void ForEachIndex<T>(this IList<T> list, Action<T, int> handler)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Action<T, System.Int32> handler
Type Parameters
Name Description
T

FromBson<T>(IList<T>, BSONArray)

Declaration
public static void FromBson<T>(this IList<T> list, BSONArray bsonArray)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
BSONArray bsonArray
Type Parameters
Name Description
T

GetAtIndexOrDefault<T>(T[], Int32)

Declaration
public static T GetAtIndexOrDefault<T>(this T[] array, int index)
Parameters
Type Name Description
T[] array
System.Int32 index
Returns
Type Description
T
Type Parameters
Name Description
T

GetAtIndexOrDefault<T>(IList<T>, Int32)

Declaration
public static T GetAtIndexOrDefault<T>(this IList<T> list, int index)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Int32 index
Returns
Type Description
T
Type Parameters
Name Description
T

GetOrAdd<T>(IList<T>, Func<T, Boolean>)

Declaration
public static T GetOrAdd<T>(this IList<T> list, Func<T, bool> match)
    where T : new()
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Func<T, System.Boolean> match
Returns
Type Description
T
Type Parameters
Name Description
T

GetOrAdd<T>(IList<T>, Func<T, Boolean>, Func<T>)

Declaration
public static T GetOrAdd<T>(this IList<T> list, Func<T, bool> match, Func<T> create)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Func<T, System.Boolean> match
System.Func<T> create
Returns
Type Description
T
Type Parameters
Name Description
T

IndexOf<T>(IEnumerable<T>, Predicate<T>)

Declaration
public static int IndexOf<T>(this IEnumerable<T> list, Predicate<T> test)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> list
System.Predicate<T> test
Returns
Type Description
System.Int32
Type Parameters
Name Description
T

MaxObj<T>(IList<T>, Func<T, Double>)

Declaration
public static T MaxObj<T>(this IList<T> list, Func<T, double> distanceFunc)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Func<T, System.Double> distanceFunc
Returns
Type Description
T
Type Parameters
Name Description
T

MaxObj<T>(IList<T>, Func<T, Single>)

Declaration
public static T MaxObj<T>(this IList<T> list, Func<T, float> distanceFunc)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Func<T, System.Single> distanceFunc
Returns
Type Description
T
Type Parameters
Name Description
T

Median<T>(IList<T>, Boolean)

Returns median value for list. If sorted is false then it makes sorted copy of list to find the median otherwise it just returns medium element.

Declaration
public static T Median<T>(this IList<T> list, bool sorted = false)
    where T : IComparable<T>
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Boolean sorted
Returns
Type Description
T
Type Parameters
Name Description
T

MinIndex<T>(IList<T>)

Declaration
public static T MinIndex<T>(this IList<T> list)
    where T : IComparable
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
Returns
Type Description
T
Type Parameters
Name Description
T

MinIndex<T>(IList<T>, IComparer<T>)

Declaration
public static int MinIndex<T>(this IList<T> list, IComparer<T> comparer)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Collections.Generic.IComparer<T> comparer
Returns
Type Description
System.Int32
Type Parameters
Name Description
T

MinObj<T>(IList<T>, Func<T, Single>)

Declaration
public static T MinObj<T>(this IList<T> list, Func<T, float> distanceFunc)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Func<T, System.Single> distanceFunc
Returns
Type Description
T
Type Parameters
Name Description
T

NullIndices<T>(IEnumerable<T>)

Returns an enumerable of each index that has a null value in the passed list.

Declaration
public static IEnumerable<int> NullIndices<T>(this IEnumerable<T> list)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> list
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Int32>
Type Parameters
Name Description
T

Random<T>(IList<T>)

Declaration
public static T Random<T>(this IList<T> list)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
Returns
Type Description
T
Type Parameters
Name Description
T

Random<T>(IList<T>, Random)

Declaration
public static T Random<T>(this IList<T> list, Random random)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Random random
Returns
Type Description
T
Type Parameters
Name Description
T

RandomWeighted<T>(IEnumerable<T>, Func<T, Single>)

Picks random element from list based on weights

Declaration
public static T RandomWeighted<T>(this IEnumerable<T> source, Func<T, float> weightSelector)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> source
System.Func<T, System.Single> weightSelector

weight to be evaluated

Returns
Type Description
T
Type Parameters
Name Description
T

RemoveAll<T, TPredicate>(List<T>, TPredicate, Action<T>)

Removes first item in list matching matcher and calls onRemove action if provided for the removed item. This method may be used with allocation-free struct based predicates instead of closures. Also it may inline predicate body for better performance.

Declaration
public static void RemoveAll<T, TPredicate>(this List<T> list, TPredicate matcher, Action<T> onRemove = null)
    where TPredicate : IPredicate<T>
Parameters
Type Name Description
System.Collections.Generic.List<T> list
TPredicate matcher
System.Action<T> onRemove
Type Parameters
Name Description
T
TPredicate

RemoveFirst<T>(IList<T>, Predicate<T>)

Removes first item matching the matcher. Returns true if item was removed.

Declaration
public static bool RemoveFirst<T>(this IList<T> list, Predicate<T> matcher)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Predicate<T> matcher
Returns
Type Description
System.Boolean
Type Parameters
Name Description
T

RemoveFirst<T, TPredicate>(List<T>, TPredicate, Action<T>)

Removes first item in list matching matcher and calls onRemove action if provided for the removed item. This method may be used with allocation-free struct based predicates instead of closures. Also it may inline predicate body for better performance.

Declaration
public static bool RemoveFirst<T, TPredicate>(this List<T> list, TPredicate matcher, Action<T> onRemove = null)
    where TPredicate : IPredicate<T>
Parameters
Type Name Description
System.Collections.Generic.List<T> list
TPredicate matcher
System.Action<T> onRemove
Returns
Type Description
System.Boolean
Type Parameters
Name Description
T
TPredicate

RotateLeft<T>(IList<T>, Int32)

"Rotates" list by offset. Every element has it's index shifted left by offset, if it results in negative index then it pushes to end of the list.

Declaration
public static IEnumerable<T> RotateLeft<T>(this IList<T> list, int offset)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Int32 offset
Returns
Type Description
System.Collections.Generic.IEnumerable<T>
Type Parameters
Name Description
T
Examples
            new[] { 1, 2, 3, 4 }.RotateLeft(2) // => 3, 4, 1, 2

SplitOnPredicate<T>(IEnumerable<T>, Func<T, Boolean>)

Declaration
public static (List<T>, List<T>) SplitOnPredicate<T>(this IEnumerable<T> list, Func<T, bool> predicate)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> list
System.Func<T, System.Boolean> predicate
Returns
Type Description
System.ValueTuple<System.Collections.Generic.List<T>, System.Collections.Generic.List<T>>
Type Parameters
Name Description
T

TryGetFirst<T>(IList<T>, Predicate<T>, out T)

Declaration
public static bool TryGetFirst<T>(this IList<T> list, Predicate<T> func, out T value)
Parameters
Type Name Description
System.Collections.Generic.IList<T> list
System.Predicate<T> func
T value
Returns
Type Description
System.Boolean
Type Parameters
Name Description
T

Types<T>(IEnumerable<T>)

Declaration
public static IEnumerable<Type> Types<T>(this IEnumerable<T> set)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> set
Returns
Type Description
System.Collections.Generic.IEnumerable<System.Type>
Type Parameters
Name Description
T

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

Declaration
public static void ZipApply<T>(this IEnumerable<T> left, IEnumerable<T> right, Action<T, T> zipFunc)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T> left
System.Collections.Generic.IEnumerable<T> right
System.Action<T, T> zipFunc
Type Parameters
Name Description
T

ZipTuples<T1, T2>(IEnumerable<T1>, IEnumerable<T2>)

Declaration
public static IEnumerable<(T1, T2)> ZipTuples<T1, T2>(this IEnumerable<T1> list1, IEnumerable<T2> list2)
Parameters
Type Name Description
System.Collections.Generic.IEnumerable<T1> list1
System.Collections.Generic.IEnumerable<T2> list2
Returns
Type Description
System.Collections.Generic.IEnumerable<System.ValueTuple<T1, T2>>
Type Parameters
Name Description
T1
T2

ZipTuples<T1, T2>(IList<T1>, IList<T2>)

Declaration
public static List<(T1, T2)> ZipTuples<T1, T2>(this IList<T1> list1, IList<T2> list2)
Parameters
Type Name Description
System.Collections.Generic.IList<T1> list1
System.Collections.Generic.IList<T2> list2
Returns
Type Description
System.Collections.Generic.List<System.ValueTuple<T1, T2>>
Type Parameters
Name Description
T1
T2
☀
☾
In This Article
Back to top
Copyright (c) Strange Loop Games 2021
☀
☾