Table of Contents

Class ListExtensions

Namespace
SunamoExtensions
Assembly
SunamoExtensions.dll

Extension methods for List type

public static class ListExtensions
Inheritance
ListExtensions
Inherited Members

Methods

AddOrSet<T>(IList<T>, int, T)

Adds or sets an item at the specified index (sets if index exists, adds if not)

public static List<T> AddOrSet<T>(this IList<T> list, int index, T item)

Parameters

list IList<T>

List to modify

index int

Index to add or set at

item T

Item to add or set

Returns

List<T>

Modified list

Type Parameters

T

Type of elements in the list

AddRangeIfNotContain<T>(IList<T>, List<T>)

Adds multiple items to the list only if they don't already exist

public static void AddRangeIfNotContain<T>(this IList<T> list, List<T> itemsToAdd)

Parameters

list IList<T>

List to add items to

itemsToAdd List<T>

Items to add

Type Parameters

T

Type of elements in the list

Insert<T>(IList<T>, int, T)

Inserts an item at the specified index

public static List<T> Insert<T>(this IList<T> list, int index, T item)

Parameters

list IList<T>

List to insert into

index int

Index to insert at

item T

Item to insert

Returns

List<T>

Modified list

Type Parameters

T

Type of elements in the list

Leading(List<string>, string)

Inserts an item at the beginning of the list NOTE: Leading items should be always the last in code, Add items should be first in code

public static List<string> Leading(this List<string> list, string item)

Parameters

list List<string>

List to modify

item string

Item to insert at the beginning

Returns

List<string>

Modified list

LeadingRange(List<string>, IList<string>)

Inserts items at the beginning of the list (direct edit)

public static List<string> LeadingRange(this List<string> list, IList<string> items)

Parameters

list List<string>

List to modify

items IList<string>

Items to insert at the beginning

Returns

List<string>

Modified list

NthOrderStatistic<T>(IList<T>, int, Random?)

Returns Nth smallest element from the list. Here n starts from 0 so that n=0 returns minimum, n=1 returns 2nd smallest element etc. Note: specified list would be mutated in the process. Reference: Introduction to Algorithms 3rd Edition, Corman et al, pp 216

public static T NthOrderStatistic<T>(this IList<T> list, int n, Random? rnd = null) where T : IComparable<T>

Parameters

list IList<T>

List to find Nth order statistic in

n int

Order statistic to find (0-based, 0 = minimum)

rnd Random

Optional random number generator

Returns

T

Nth smallest element

Type Parameters

T

Type of elements that implements IComparable

Partition<T>(IList<T>, int, int, Random?)

Partitions the given list around a pivot element such that all elements on left of pivot are <= pivot and the ones at the right are > pivot. This method can be used for sorting, N-order statistics such as median finding algorithms. Pivot is selected randomly if random number generator is supplied else its selected as last element in the list. Reference: Introduction to Algorithms 3rd Edition, Corman et al, pp 171

public static int Partition<T>(this IList<T> list, int start, int end, Random? rnd = null) where T : IComparable<T>

Parameters

list IList<T>

List to partition (must be List due to working with indexes)

start int

Start index

end int

End index

rnd Random

Optional random number generator for pivot selection

Returns

int

Index of the pivot element after partitioning

Type Parameters

T

Type of elements that implements IComparable

RemoveMany(IList<string>, List<string>)

Removes multiple string items from the list

public static void RemoveMany(this IList<string> list, List<string> itemsToRemove)

Parameters

list IList<string>

List to remove items from

itemsToRemove List<string>

Items to remove

RemoveMany<T>(IList<T>, List<T>)

Removes multiple items from the list

public static void RemoveMany<T>(this IList<T> list, List<T> itemsToRemove)

Parameters

list IList<T>

List to remove items from

itemsToRemove List<T>

Items to remove

Type Parameters

T

Type of elements in the list