Table of Contents

Class CollectionHelper

Namespace
Ecng.Collections
Assembly
Ecng.Collections.dll

Provides extension methods and helper utilities for working with collections.

public static class CollectionHelper
Inheritance
CollectionHelper
Inherited Members

Methods

AddRange(BitArray, params bool[])

Adds a range of bits to an existing BitArray.

public static void AddRange(this BitArray array, params bool[] bits)

Parameters

array BitArray

The BitArray to extend.

bits bool[]

The bits to add.

Exceptions

ArgumentNullException

Thrown when array or bits is null.

AddRange<T>(ICollection<T>, IEnumerable<T>)

Adds a range of items to a collection, using optimized methods when available.

public static void AddRange<T>(this ICollection<T> source, IEnumerable<T> items)

Parameters

source ICollection<T>

The collection to add items to.

items IEnumerable<T>

The items to add.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when source or items is null.

Add<TKey, TValue>(IDictionary<TKey, TValue>, (TKey key, TValue value))

Adds a value tuple as a key-value pair to a dictionary.

public static void Add<TKey, TValue>(this IDictionary<TKey, TValue> dict, (TKey key, TValue value) tuple)

Parameters

dict IDictionary<TKey, TValue>

The dictionary to add to.

tuple (TKey key, TValue value)

The value tuple to add.

Type Parameters

TKey

The type of the key.

TValue

The type of the value.

Append2<T>(IEnumerable<T>, T)

Appends a single value to the end of a sequence.

public static IEnumerable<T> Append2<T>(this IEnumerable<T> values, T value)

Parameters

values IEnumerable<T>

The sequence to append to.

value T

The value to append.

Returns

IEnumerable<T>

An enumerable with the value appended.

Type Parameters

T

The type of elements in the sequence.

Batch<T>(IEnumerable<T>, int)

Splits a sequence into batches of a specified size.

public static IEnumerable<T[]> Batch<T>(this IEnumerable<T> source, int size)

Parameters

source IEnumerable<T>

The sequence to batch.

size int

The size of each batch.

Returns

IEnumerable<T[]>

An enumerable of arrays, each containing up to size elements.

Type Parameters

T

The type of elements in the sequence.

Batch<TSource, TResult>(IEnumerable<TSource>, int, Func<IEnumerable<TSource>, TResult>, Func<bool>)

Splits a sequence into batches with custom result selection and stopping condition.

public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSource> source, int size, Func<IEnumerable<TSource>, TResult> resultSelector, Func<bool> needStop)

Parameters

source IEnumerable<TSource>

The sequence to batch.

size int

The maximum size of each batch.

resultSelector Func<IEnumerable<TSource>, TResult>

The function to transform each batch into a result.

needStop Func<bool>

The function to determine if batching should stop prematurely.

Returns

IEnumerable<TResult>

An enumerable of results, each representing a batch.

Type Parameters

TSource

The type of elements in the sequence.

TResult

The type of the result for each batch.

Bind<T>(INotifyList<T>, IList<T>)

Binds a source notifying list to a destination list, synchronizing additions, removals, insertions, and clear operations.

public static void Bind<T>(this INotifyList<T> source, IList<T> destination)

Parameters

source INotifyList<T>

The source list that notifies changes.

destination IList<T>

The destination list to synchronize with.

Type Parameters

T

The type of elements in the lists.

Exceptions

ArgumentNullException

Thrown when source or destination is null.

ConcatEx<T, TItem>(T, T)

Concatenates two collections of the same type into a new instance.

public static T ConcatEx<T, TItem>(this T first, T second) where T : ICollection<TItem>, new()

Parameters

first T

The first collection.

second T

The second collection.

Returns

T

A new collection containing all elements from both input collections.

Type Parameters

T

The type of the collection, which must implement ICollection<T> and have a parameterless constructor.

TItem

The type of elements in the collection.

CopyAndClear<T>(ICollection<T>)

Copies all items from a collection to an array and clears the collection.

public static T[] CopyAndClear<T>(this ICollection<T> items)

Parameters

items ICollection<T>

The collection to copy and clear.

Returns

T[]

An array containing the copied items.

Type Parameters

T

The type of elements in the collection.

CopyTo<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>)

Copies key-value pairs from a sequence to a dictionary.

public static void CopyTo<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source, IDictionary<TKey, TValue> destination)

Parameters

source IEnumerable<KeyValuePair<TKey, TValue>>

The sequence of key-value pairs.

destination IDictionary<TKey, TValue>

The dictionary to copy to.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when source or destination is null.

Count2(IEnumerable)

Counts the number of elements in a non-generic sequence.

public static int Count2(this IEnumerable source)

Parameters

source IEnumerable

The sequence to count.

Returns

int

The number of elements in the sequence.

DamerauLevenshteinDistance<T>(T[], T[], int)

Computes the Damerau-Levenshtein distance between two sequences, with a threshold to limit computation.

public static int DamerauLevenshteinDistance<T>(T[] source, T[] target, int threshold) where T : IEquatable<T>

Parameters

source T[]

The first sequence.

target T[]

The second sequence.

threshold int

The maximum distance to compute; returns int.MaxValue if exceeded.

Returns

int

The Damerau-Levenshtein distance, or int.MaxValue if it exceeds the threshold.

Type Parameters

T

The type of elements in the sequences, which must implement IEquatable<T>.

Exceptions

ArgumentNullException

Thrown when source or target is null.

ElementAtFromEndOrDefault<T>(SynchronizedLinkedList<T>, int)

Retrieves an element from a synchronized linked list counting from the end, or the default value if out of range.

public static T ElementAtFromEndOrDefault<T>(this SynchronizedLinkedList<T> list, int index)

Parameters

list SynchronizedLinkedList<T>

The synchronized linked list to query.

index int

The zero-based index from the end (0 is the last element).

Returns

T

The element at the specified position from the end, or the default value if out of range.

Type Parameters

T

The type of elements in the linked list.

Exceptions

ArgumentNullException

Thrown when list is null.

ElementAtFromEndOrDefault<T>(IEnumerable<T>, int)

Retrieves an element from a sequence counting from the end, or the default value if out of range.

public static T ElementAtFromEndOrDefault<T>(this IEnumerable<T> source, int index)

Parameters

source IEnumerable<T>

The sequence to query.

index int

The zero-based index from the end (0 is the last element).

Returns

T

The element at the specified position from the end, or the default value if out of range.

Type Parameters

T

The type of elements in the sequence.

ElementAtFromEndOrDefault<T>(LinkedList<T>, int)

Retrieves an element from a linked list counting from the end, or the default value if out of range.

public static T ElementAtFromEndOrDefault<T>(this LinkedList<T> list, int index)

Parameters

list LinkedList<T>

The linked list to query.

index int

The zero-based index from the end (0 is the last element).

Returns

T

The element at the specified position from the end, or the default value if out of range.

Type Parameters

T

The type of elements in the linked list.

Exceptions

ArgumentNullException

Thrown when list is null.

ElementAtFromEnd<T>(SynchronizedLinkedList<T>, int)

Retrieves an element from a synchronized linked list counting from the end.

public static T ElementAtFromEnd<T>(this SynchronizedLinkedList<T> list, int index)

Parameters

list SynchronizedLinkedList<T>

The synchronized linked list to query.

index int

The zero-based index from the end (0 is the last element).

Returns

T

The element at the specified position from the end.

Type Parameters

T

The type of elements in the linked list.

Exceptions

ArgumentNullException

Thrown when list is null.

ArgumentOutOfRangeException

Thrown when the list is empty or the index is out of range.

ElementAtFromEnd<T>(IEnumerable<T>, int)

Retrieves an element from a sequence counting from the end.

public static T ElementAtFromEnd<T>(this IEnumerable<T> source, int index)

Parameters

source IEnumerable<T>

The sequence to query.

index int

The zero-based index from the end (0 is the last element).

Returns

T

The element at the specified position from the end.

Type Parameters

T

The type of elements in the sequence.

ElementAtFromEnd<T>(LinkedList<T>, int)

Retrieves an element from a linked list counting from the end.

public static T ElementAtFromEnd<T>(this LinkedList<T> list, int index)

Parameters

list LinkedList<T>

The linked list to query.

index int

The zero-based index from the end (0 is the last element).

Returns

T

The element at the specified position from the end.

Type Parameters

T

The type of elements in the linked list.

Exceptions

ArgumentNullException

Thrown when list is null.

ArgumentOutOfRangeException

Thrown when the list is empty or the index is out of range.

ElementAtOr<T>(IEnumerable<T>, int)

Returns the element at a specified index in a sequence, or null if the index is out of range (for value types).

public static T? ElementAtOr<T>(this IEnumerable<T> source, int index) where T : struct

Parameters

source IEnumerable<T>

The sequence to query.

index int

The zero-based index of the element to retrieve.

Returns

T?

The element at the specified index, or null if the index is out of range.

Type Parameters

T

The type of elements in the sequence, which must be a value type.

Exceptions

ArgumentNullException

Thrown when source is null.

ArgumentOutOfRangeException

Thrown when index is negative.

FirstOr<T>(IEnumerable<T>)

Returns the first element of a sequence, or null if the sequence is empty (for value types).

public static T? FirstOr<T>(this IEnumerable<T> source) where T : struct

Parameters

source IEnumerable<T>

The sequence to query.

Returns

T?

The first element, or null if the sequence is empty.

Type Parameters

T

The type of elements in the sequence, which must be a value type.

Exceptions

ArgumentNullException

Thrown when source is null.

FirstOr<T>(IEnumerable<T>, T)

Returns the first element of a sequence, or a specified alternate value if the sequence is empty.

public static T FirstOr<T>(this IEnumerable<T> source, T alternate)

Parameters

source IEnumerable<T>

The sequence to query.

alternate T

The value to return if the sequence is empty.

Returns

T

The first element, or alternate if the sequence is empty.

Type Parameters

T

The type of elements in the sequence.

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

Applies an action to each element in a sequence.

public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)

Parameters

source IEnumerable<T>

The sequence to iterate over.

action Action<T>

The action to apply to each element.

Type Parameters

T

The type of elements in the sequence.

Exceptions

ArgumentNullException

Thrown when source or action is null.

FromBits(bool[])

Converts an array of bits to an integer starting from the beginning.

public static int FromBits(this bool[] bits)

Parameters

bits bool[]

The array of bits to convert.

Returns

int

The integer value represented by the bits.

FromBits(bool[], int)

Converts an array of bits to an integer starting from a specified position.

public static int FromBits(this bool[] bits, int startBit)

Parameters

bits bool[]

The array of bits to convert.

startBit int

The starting bit position (0-based).

Returns

int

The integer value represented by the bits.

FromBits2(bool[])

Converts an array of bits to a long integer starting from the beginning.

public static long FromBits2(this bool[] bits)

Parameters

bits bool[]

The array of bits to convert.

Returns

long

The long integer value represented by the bits.

FromBits2(bool[], int)

Converts an array of bits to a long integer starting from a specified position.

public static long FromBits2(this bool[] bits, int startBit)

Parameters

bits bool[]

The array of bits to convert.

startBit int

The starting bit position (0-based).

Returns

long

The long integer value represented by the bits.

Exceptions

ArgumentNullException

Thrown when bits is null.

ArgumentOutOfRangeException

Thrown when startBit is greater than or equal to the length of bits.

GetAndRemove<TKey, TValue>(IDictionary<TKey, TValue>, TKey)

Retrieves a value from a dictionary by key and removes the key-value pair.

public static TValue GetAndRemove<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key)

Parameters

dict IDictionary<TKey, TValue>

The dictionary to operate on.

key TKey

The key of the value to retrieve and remove.

Returns

TValue

The value associated with the key.

Type Parameters

TKey

The type of keys in the dictionary.

TValue

The type of values in the dictionary.

GetHashCodeEx<T>(IEnumerable<T>)

Computes a hash code for a sequence of elements.

public static int GetHashCodeEx<T>(this IEnumerable<T> collection)

Parameters

collection IEnumerable<T>

The sequence to compute the hash code for.

Returns

int

A hash code for the sequence.

Type Parameters

T

The type of elements in the sequence.

Exceptions

ArgumentNullException

Thrown when collection is null.

GetKeys<TKey, TValue>(SynchronizedDictionary<TKey, TValue>, TValue)

Retrieves all keys in a synchronized dictionary that map to a specific value.

public static IEnumerable<TKey> GetKeys<TKey, TValue>(this SynchronizedDictionary<TKey, TValue> dictionary, TValue value)

Parameters

dictionary SynchronizedDictionary<TKey, TValue>

The synchronized dictionary to query.

value TValue

The value to match.

Returns

IEnumerable<TKey>

An enumerable of keys associated with the specified value.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

GetKeys<TKey, TValue>(IDictionary<TKey, TValue>, TValue)

Retrieves all keys in a dictionary that map to a specific value.

public static IEnumerable<TKey> GetKeys<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TValue value)

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to query.

value TValue

The value to match.

Returns

IEnumerable<TKey>

An enumerable of keys associated with the specified value.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

HasNullItem<T>(IEnumerable<T>)

Determines whether a sequence contains any null items.

public static bool HasNullItem<T>(this IEnumerable<T> items) where T : class

Parameters

items IEnumerable<T>

The sequence to check.

Returns

bool

True if the sequence contains null; otherwise, false.

Type Parameters

T

The type of elements in the sequence, which must be a reference type.

IndexOf<T>(IEnumerable<T>, Func<T, bool>)

Returns the index of the first element in a sequence that satisfies a specified condition.

public static int IndexOf<T>(this IEnumerable<T> source, Func<T, bool> predicate)

Parameters

source IEnumerable<T>

The sequence to search.

predicate Func<T, bool>

The condition to test each element against.

Returns

int

The zero-based index of the first matching element, or -1 if no element is found.

Type Parameters

T

The type of elements in the sequence.

IsEmpty(IEnumerable<char>)

Determines whether a sequence of characters is empty.

[Obsolete("Use StringHelper.IsEmpty.")]
public static bool IsEmpty(this IEnumerable<char> source)

Parameters

source IEnumerable<char>

The sequence of characters to check.

Returns

bool

True if the sequence is null or empty; otherwise, false.

IsEmpty<T>(ICollection<T>)

Determines whether a collection is empty.

public static bool IsEmpty<T>(this ICollection<T> source)

Parameters

source ICollection<T>

The collection to check.

Returns

bool

True if the collection is empty; otherwise, false.

Type Parameters

T

The type of elements in the collection.

IsEmpty<T>(IEnumerable<T>)

Determines whether a sequence is empty.

public static bool IsEmpty<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The sequence to check.

Returns

bool

True if the sequence is empty; otherwise, false.

Type Parameters

T

The type of elements in the sequence.

IsEmpty<T>(IEnumerable<T>, Func<T, bool>)

Determines whether a sequence is empty based on a predicate.

public static bool IsEmpty<T>(this IEnumerable<T> source, Func<T, bool> predicate)

Parameters

source IEnumerable<T>

The sequence to check.

predicate Func<T, bool>

The condition to test each element against.

Returns

bool

True if no elements match the predicate; otherwise, false.

Type Parameters

T

The type of elements in the sequence.

IsEmpty<T>(T[])

Determines whether an array is empty.

public static bool IsEmpty<T>(this T[] source)

Parameters

source T[]

The array to check.

Returns

bool

True if the array is empty; otherwise, false.

Type Parameters

T

The type of elements in the array.

Exceptions

ArgumentNullException

Thrown when source is null.

LastOr<T>(IEnumerable<T>)

Returns the last element of a sequence, or null if the sequence is empty (for value types).

public static T? LastOr<T>(this IEnumerable<T> source) where T : struct

Parameters

source IEnumerable<T>

The sequence to query.

Returns

T?

The last element, or null if the sequence is empty.

Type Parameters

T

The type of elements in the sequence, which must be a value type.

Exceptions

ArgumentNullException

Thrown when source is null.

OrderByDescending<T>(IEnumerable<T>)

Orders a sequence of values in descending order using the default comparer.

public static IEnumerable<T> OrderByDescending<T>(this IEnumerable<T> values)

Parameters

values IEnumerable<T>

The sequence to order.

Returns

IEnumerable<T>

An ordered sequence of the input values in descending order.

Type Parameters

T

The type of elements in the sequence.

OrderBy<T>(IEnumerable<T>)

Orders a sequence of values in ascending order using the default comparer.

public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> values)

Parameters

values IEnumerable<T>

The sequence to order.

Returns

IEnumerable<T>

An ordered sequence of the input values.

Type Parameters

T

The type of elements in the sequence.

OrderBy<T>(IEnumerable<T>, Comparison<T>)

Sorts a collection using a Comparison<T> delegate.

public static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> collection, Comparison<T> comparison)

Parameters

collection IEnumerable<T>

The collection to sort.

comparison Comparison<T>

The comparison delegate to determine order.

Returns

IOrderedEnumerable<T>

An ordered enumerable of the collection.

Type Parameters

T

The type of elements in the collection.

Permutations<TKey, TValue>(IEnumerable<TKey>, Func<TKey, IEnumerable<TValue>>)

Generates all possible permutations of values selected from keys.

public static IEnumerable<TValue[]> Permutations<TKey, TValue>(this IEnumerable<TKey> keys, Func<TKey, IEnumerable<TValue>> selector)

Parameters

keys IEnumerable<TKey>

The sequence of keys.

selector Func<TKey, IEnumerable<TValue>>

The function to select possible values for each key.

Returns

IEnumerable<TValue[]>

An enumerable of arrays representing all permutations.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when keys or selector is null.

RemoveRange<T>(ICollection<T>, IEnumerable<T>)

Removes a range of items from a collection, using optimized methods when available.

public static void RemoveRange<T>(this ICollection<T> source, IEnumerable<T> items)

Parameters

source ICollection<T>

The collection to remove items from.

items IEnumerable<T>

The items to remove.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when items is null.

RemoveWhere2<T>(IList<T>, Func<T, bool>)

Removes items from a list that match a specified filter and adjusts the list size.

public static int RemoveWhere2<T>(this IList<T> list, Func<T, bool> filter)

Parameters

list IList<T>

The list to remove items from.

filter Func<T, bool>

The condition to identify items to remove.

Returns

int

The number of items removed.

Type Parameters

T

The type of elements in the list.

RemoveWhere<T>(ICollection<T>, Func<T, bool>)

Removes items from a collection that match a specified filter and returns the removed items.

public static IEnumerable<T> RemoveWhere<T>(this ICollection<T> collection, Func<T, bool> filter)

Parameters

collection ICollection<T>

The collection to remove items from.

filter Func<T, bool>

The condition to identify items to remove.

Returns

IEnumerable<T>

An enumerable of the removed items.

Type Parameters

T

The type of elements in the collection.

SafeAddAsync<TKey, TValue>(IDictionary<TKey, TaskCompletionSource<TValue>>, AsyncReaderWriterLock, TKey, Func<TKey, CancellationToken, Task<TValue>>, CancellationToken)

Asynchronously adds a new key to a dictionary of task completion sources, ensuring only one task is created per key, using a reader-writer lock.

[CLSCompliant(false)]
public static Task<TValue> SafeAddAsync<TKey, TValue>(this IDictionary<TKey, TaskCompletionSource<TValue>> dictionary, AsyncReaderWriterLock sync, TKey key, Func<TKey, CancellationToken, Task<TValue>> handler, CancellationToken cancellationToken)

Parameters

dictionary IDictionary<TKey, TaskCompletionSource<TValue>>

The dictionary mapping keys to task completion sources.

sync AsyncReaderWriterLock

The reader-writer lock for synchronization.

key TKey

The key to add or retrieve.

handler Func<TKey, CancellationToken, Task<TValue>>

The asynchronous function to generate a value if the key is new.

cancellationToken CancellationToken

The cancellation token to cancel the operation.

Returns

Task<TValue>

The task representing the value for the key.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when dictionary, sync, or handler is null.

SafeAddAsync<TKey, TValue>(IDictionary<TKey, TaskCompletionSource<TValue>>, TKey, Func<TKey, CancellationToken, Task<TValue>>, CancellationToken)

Asynchronously adds a new key to a dictionary of task completion sources, ensuring only one task is created per key, using synchronized locking.

public static Task<TValue> SafeAddAsync<TKey, TValue>(this IDictionary<TKey, TaskCompletionSource<TValue>> dictionary, TKey key, Func<TKey, CancellationToken, Task<TValue>> handler, CancellationToken cancellationToken)

Parameters

dictionary IDictionary<TKey, TaskCompletionSource<TValue>>

The dictionary mapping keys to task completion sources.

key TKey

The key to add or retrieve.

handler Func<TKey, CancellationToken, Task<TValue>>

The asynchronous function to generate a value if the key is new.

cancellationToken CancellationToken

The cancellation token to cancel the operation.

Returns

Task<TValue>

The task representing the value for the key.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when dictionary or handler is null.

SafeAddAsync<TKey, TValue>(IDictionary<TKey, TValue>, AsyncReaderWriterLock, TKey, Func<TKey, CancellationToken, Task<TValue>>, CancellationToken)

Asynchronously adds a new key to a dictionary with a value generated by a handler if it doesn't exist, using a reader-writer lock.

[CLSCompliant(false)]
public static Task<TValue> SafeAddAsync<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, AsyncReaderWriterLock sync, TKey key, Func<TKey, CancellationToken, Task<TValue>> handler, CancellationToken cancellationToken)

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to operate on.

sync AsyncReaderWriterLock

The reader-writer lock for synchronization.

key TKey

The key to add or retrieve.

handler Func<TKey, CancellationToken, Task<TValue>>

The asynchronous function to generate a value if the key is new.

cancellationToken CancellationToken

The cancellation token to cancel the operation.

Returns

Task<TValue>

The existing or newly created value.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when dictionary, sync, or handler is null.

SafeAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey)

Adds a new key to a dictionary with a default value if it doesn't exist, and returns the value.

public static TValue SafeAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to operate on.

key TKey

The key to add or retrieve.

Returns

TValue

The existing or newly created value.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

SafeAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, out bool)

Adds a new key to a dictionary with a default value if it doesn't exist, indicating whether it was new.

public static TValue SafeAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, out bool isNew)

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to operate on.

key TKey

The key to add or retrieve.

isNew bool

When this method returns, indicates whether the key was newly added.

Returns

TValue

The existing or newly created value.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

SafeAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Func<TKey, TValue>)

Adds a new key to a dictionary with a value generated by a handler if it doesn't exist.

public static TValue SafeAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> handler)

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to operate on.

key TKey

The key to add or retrieve.

handler Func<TKey, TValue>

The function to generate a value if the key is new.

Returns

TValue

The existing or newly created value.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when dictionary or handler is null.

SafeAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Func<TKey, TValue>, out bool)

Adds a new key to a dictionary with a value generated by a handler if it doesn't exist, indicating whether it was new.

public static TValue SafeAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> handler, out bool isNew)

Parameters

dictionary IDictionary<TKey, TValue>

The dictionary to operate on.

key TKey

The key to add or retrieve.

handler Func<TKey, TValue>

The function to generate a value if the key is new.

isNew bool

When this method returns, indicates whether the key was newly added.

Returns

TValue

The existing or newly created value.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when dictionary or handler is null.

SelectManyAsync<T, T1>(IEnumerable<T>, Func<T, Task<IEnumerable<T1>>>)

Asynchronously flattens a sequence of tasks that each return a sequence into a single sequence.

public static Task<IEnumerable<T1>> SelectManyAsync<T, T1>(this IEnumerable<T> enumeration, Func<T, Task<IEnumerable<T1>>> func)

Parameters

enumeration IEnumerable<T>

The source sequence of elements.

func Func<T, Task<IEnumerable<T1>>>

The asynchronous function to transform each element into a sequence.

Returns

Task<IEnumerable<T1>>

A task that resolves to a flattened sequence of results.

Type Parameters

T

The type of elements in the source sequence.

T1

The type of elements in the resulting sequence.

SelectMany<T>(IEnumerable<IEnumerable<T>>)

Flattens a sequence of sequences into a single sequence.

public static IEnumerable<T> SelectMany<T>(this IEnumerable<IEnumerable<T>> values)

Parameters

values IEnumerable<IEnumerable<T>>

The sequence of sequences to flatten.

Returns

IEnumerable<T>

A single sequence containing all elements from the input sequences.

Type Parameters

T

The type of elements in the sequences.

SequenceEqual<T>(IEnumerable<T>, IEnumerable<T>, Func<T, T, bool>)

Determines whether two sequences are equal using a custom equality comparer function.

public static bool SequenceEqual<T>(this IEnumerable<T> first, IEnumerable<T> second, Func<T, T, bool> comparer)

Parameters

first IEnumerable<T>

The first sequence to compare.

second IEnumerable<T>

The second sequence to compare.

comparer Func<T, T, bool>

The function to determine equality between elements.

Returns

bool

True if the sequences are equal; otherwise, false.

Type Parameters

T

The type of elements in the sequences.

SingleWhenOnly<T>(IEnumerable<T>)

Returns the single element of a sequence if it contains exactly one element, otherwise returns the default value.

public static T SingleWhenOnly<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The sequence to query.

Returns

T

The single element if the sequence has exactly one; otherwise, the default value of T.

Type Parameters

T

The type of elements in the sequence.

SyncDo<TCollection>(TCollection, Action<TCollection>)

Executes an action on a synchronized collection with thread-safe access.

public static void SyncDo<TCollection>(this TCollection collection, Action<TCollection> action) where TCollection : class, ISynchronizedCollection

Parameters

collection TCollection

The synchronized collection to operate on.

action Action<TCollection>

The action to execute.

Type Parameters

TCollection

The type of the synchronized collection.

Exceptions

ArgumentNullException

Thrown when collection or action is null.

SyncGet<TCollection, TResult>(TCollection, Func<TCollection, TResult>)

Executes a function on a synchronized collection with thread-safe access.

public static TResult SyncGet<TCollection, TResult>(this TCollection collection, Func<TCollection, TResult> func) where TCollection : class, ISynchronizedCollection

Parameters

collection TCollection

The synchronized collection to operate on.

func Func<TCollection, TResult>

The function to execute.

Returns

TResult

The result of the function.

Type Parameters

TCollection

The type of the synchronized collection.

TResult

The type of the result.

Exceptions

ArgumentNullException

Thrown when collection or func is null.

Sync<T>(HashSet<T>)

Converts a hash set to a synchronized set.

public static SynchronizedSet<T> Sync<T>(this HashSet<T> list)

Parameters

list HashSet<T>

The hash set to synchronize.

Returns

SynchronizedSet<T>

A SynchronizedSet<T> containing the set's elements, or null if list is null.

Type Parameters

T

The type of elements in the set.

Sync<T>(IList<T>)

Converts a list to a synchronized list, or returns it if already synchronized.

public static SynchronizedList<T> Sync<T>(this IList<T> list)

Parameters

list IList<T>

The list to synchronize.

Returns

SynchronizedList<T>

A SynchronizedList<T> containing the list's elements, or null if list is null.

Type Parameters

T

The type of elements in the list.

Sync<TKey, TValue>(IDictionary<TKey, TValue>)

Converts a dictionary to a synchronized dictionary, or returns it if already synchronized.

public static SynchronizedDictionary<TKey, TValue> Sync<TKey, TValue>(this IDictionary<TKey, TValue> dict)

Parameters

dict IDictionary<TKey, TValue>

The dictionary to synchronize.

Returns

SynchronizedDictionary<TKey, TValue>

A SynchronizedDictionary<TKey, TValue> containing the dictionary's elements, or null if dict is null.

Type Parameters

TKey

The type of keys in the dictionary.

TValue

The type of values in the dictionary.

ToBits(double)

Converts a double value to its binary representation as an array of bits.

public static bool[] ToBits(this double value)

Parameters

value double

The double value to convert.

Returns

bool[]

An array of 64 bits representing the value.

ToBits(double, int)

Converts a double value to a specified number of bits.

public static bool[] ToBits(this double value, int count)

Parameters

value double

The double value to convert.

count int

The number of bits to return.

Returns

bool[]

An array of bits representing the value.

ToBits(double, int, int)

Converts a double value to a specified range of bits.

public static bool[] ToBits(this double value, int startBit, int bitCount)

Parameters

value double

The double value to convert.

startBit int

The starting bit position (0-based).

bitCount int

The number of bits to return.

Returns

bool[]

An array of bits representing the specified range.

ToBits(int)

Converts an int value to its binary representation as an array of bits.

public static bool[] ToBits(this int value)

Parameters

value int

The int value to convert.

Returns

bool[]

An array of 32 bits representing the value.

ToBits(int, int)

Converts an int value to a specified number of bits.

public static bool[] ToBits(this int value, int bitCount)

Parameters

value int

The int value to convert.

bitCount int

The number of bits to return.

Returns

bool[]

An array of bits representing the value.

ToBits(int, int, int)

Converts an int value to a specified range of bits.

public static bool[] ToBits(this int value, int startBit, int bitCount)

Parameters

value int

The int value to convert.

startBit int

The starting bit position (0-based).

bitCount int

The number of bits to return.

Returns

bool[]

An array of bits representing the specified range.

Exceptions

ArgumentOutOfRangeException

Thrown when startBit is outside [0, 31] or bitCount is negative or exceeds 32 when added to startBit.

ToBits(long)

Converts a long value to its binary representation as an array of bits.

public static bool[] ToBits(this long value)

Parameters

value long

The long value to convert.

Returns

bool[]

An array of 64 bits representing the value.

ToBits(long, int)

Converts a long value to a specified number of bits.

public static bool[] ToBits(this long value, int bitCount)

Parameters

value long

The long value to convert.

bitCount int

The number of bits to return.

Returns

bool[]

An array of bits representing the value.

ToBits(long, int, int)

Converts a long value to a specified range of bits.

public static bool[] ToBits(this long value, int startBit, int bitCount)

Parameters

value long

The long value to convert.

startBit int

The starting bit position (0-based).

bitCount int

The number of bits to return.

Returns

bool[]

An array of bits representing the specified range.

ToBits(float)

Converts a float value to its binary representation as an array of bits.

public static bool[] ToBits(this float value)

Parameters

value float

The float value to convert.

Returns

bool[]

An array of 32 bits representing the value.

ToBits(float, int)

Converts a float value to a specified number of bits.

public static bool[] ToBits(this float value, int bitCount)

Parameters

value float

The float value to convert.

bitCount int

The number of bits to return.

Returns

bool[]

An array of bits representing the value.

ToBits(float, int, int)

Converts a float value to a specified range of bits.

public static bool[] ToBits(this float value, int startBit, int bitCount)

Parameters

value float

The float value to convert.

startBit int

The starting bit position (0-based).

bitCount int

The number of bits to return.

Returns

bool[]

An array of bits representing the specified range.

ToComparer<T>(Comparison<T>)

Converts a Comparison<T> delegate to an IComparer<T> instance.

public static IComparer<T> ToComparer<T>(this Comparison<T> comparer)

Parameters

comparer Comparison<T>

The comparison delegate.

Returns

IComparer<T>

An IComparer<T> instance.

Type Parameters

T

The type of objects to compare.

ToComparer<T>(Func<T, T, bool>)

Converts a function to an IEqualityComparer<T> instance.

public static IEqualityComparer<T> ToComparer<T>(this Func<T, T, bool> comparer)

Parameters

comparer Func<T, T, bool>

The function to determine equality between two objects.

Returns

IEqualityComparer<T>

An IEqualityComparer<T> instance.

Type Parameters

T

The type of objects to compare.

ToComparer<T>(Func<T, T, int>)

Converts a function to an IComparer<T> instance.

public static IComparer<T> ToComparer<T>(this Func<T, T, int> comparer)

Parameters

comparer Func<T, T, int>

The function to compare two objects.

Returns

IComparer<T>

An IComparer<T> instance.

Type Parameters

T

The type of objects to compare.

ToDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>)

Converts a sequence of key-value pairs to a dictionary using the default equality comparer.

public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)

Parameters

source IEnumerable<KeyValuePair<TKey, TValue>>

The sequence of key-value pairs.

Returns

IDictionary<TKey, TValue>

A dictionary containing the key-value pairs.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

ToDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>, IEqualityComparer<TKey>)

Converts a sequence of key-value pairs to a dictionary with a specified equality comparer.

public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey> comparer)

Parameters

source IEnumerable<KeyValuePair<TKey, TValue>>

The sequence of key-value pairs.

comparer IEqualityComparer<TKey>

The equality comparer for keys.

Returns

IDictionary<TKey, TValue>

A dictionary containing the key-value pairs.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

ToDictionary<TKey, TValue>(IEnumerable<IGrouping<TKey, TValue>>)

Converts a grouping into a dictionary where each key maps to its group of values.

public static IDictionary<TKey, IEnumerable<TValue>> ToDictionary<TKey, TValue>(this IEnumerable<IGrouping<TKey, TValue>> grouping)

Parameters

grouping IEnumerable<IGrouping<TKey, TValue>>

The grouping to convert.

Returns

IDictionary<TKey, IEnumerable<TValue>>

A dictionary mapping keys to their groups.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when grouping is null.

ToDictionary<TKey, TValue>(IEnumerable<Tuple<TKey, TValue>>)

Converts a sequence of tuples to a dictionary using the default equality comparer.

public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<Tuple<TKey, TValue>> source)

Parameters

source IEnumerable<Tuple<TKey, TValue>>

The sequence of tuples.

Returns

IDictionary<TKey, TValue>

A dictionary containing the key-value pairs from the tuples.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

ToDictionary<TKey, TValue>(IEnumerable<Tuple<TKey, TValue>>, IEqualityComparer<TKey>)

Converts a sequence of tuples to a dictionary with a specified equality comparer.

public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<Tuple<TKey, TValue>> source, IEqualityComparer<TKey> comparer)

Parameters

source IEnumerable<Tuple<TKey, TValue>>

The sequence of tuples.

comparer IEqualityComparer<TKey>

The equality comparer for keys.

Returns

IDictionary<TKey, TValue>

A dictionary containing the key-value pairs from the tuples.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

ToDictionary<TSource, TKey, TValue>(IEnumerable<TSource>, Func<TSource, int, TKey>, Func<TSource, int, TValue>)

Converts a sequence into a dictionary using selector functions for keys and values.

public static IDictionary<TKey, TValue> ToDictionary<TSource, TKey, TValue>(this IEnumerable<TSource> source, Func<TSource, int, TKey> keySelector, Func<TSource, int, TValue> valueSelector)

Parameters

source IEnumerable<TSource>

The source sequence.

keySelector Func<TSource, int, TKey>

The function to extract keys from elements.

valueSelector Func<TSource, int, TValue>

The function to extract values from elements.

Returns

IDictionary<TKey, TValue>

A dictionary containing the transformed key-value pairs.

Type Parameters

TSource

The type of elements in the source sequence.

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when source, keySelector, or valueSelector is null.

ToEx<T>(IEnumerable<T>)

Converts an enumerable to an IEnumerableEx<T> with a count determined by enumeration.

public static IEnumerableEx<T> ToEx<T>(this IEnumerable<T> values)

Parameters

values IEnumerable<T>

The enumerable to convert.

Returns

IEnumerableEx<T>

An IEnumerableEx<T> with the specified count.

Type Parameters

T

The type of elements in the enumerable.

ToEx<T>(IEnumerable<T>, int)

Converts an enumerable to an IEnumerableEx<T> with a specified count.

public static IEnumerableEx<T> ToEx<T>(this IEnumerable<T> values, int count)

Parameters

values IEnumerable<T>

The enumerable to convert.

count int

The predefined count of elements.

Returns

IEnumerableEx<T>

An IEnumerableEx<T> with the specified count.

Type Parameters

T

The type of elements in the enumerable.

ToFunc<T>(Comparison<T>)

Converts a Comparison<T> delegate to a function.

public static Func<T, T, int> ToFunc<T>(this Comparison<T> comparer)

Parameters

comparer Comparison<T>

The comparison delegate.

Returns

Func<T, T, int>

A function that compares two objects.

Type Parameters

T

The type of objects to compare.

ToIgnoreCaseSet(IEnumerable<string>)

Converts a sequence of strings to a case-insensitive set.

public static ISet<string> ToIgnoreCaseSet(this IEnumerable<string> values)

Parameters

values IEnumerable<string>

The sequence of strings to convert.

Returns

ISet<string>

A set containing the unique strings, ignoring case.

ToPairSet<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>)

Converts a sequence of key-value pairs into a PairSet<TKey, TValue> with the default equality comparer.

public static PairSet<TKey, TValue> ToPairSet<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)

Parameters

source IEnumerable<KeyValuePair<TKey, TValue>>

The sequence of key-value pairs.

Returns

PairSet<TKey, TValue>

A PairSet<TKey, TValue> containing the key-value pairs.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

ToPairSet<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>>, IEqualityComparer<TKey>)

Converts a sequence of key-value pairs into a PairSet<TKey, TValue> with a specified equality comparer.

public static PairSet<TKey, TValue> ToPairSet<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey> comparer)

Parameters

source IEnumerable<KeyValuePair<TKey, TValue>>

The sequence of key-value pairs.

comparer IEqualityComparer<TKey>

The equality comparer for keys.

Returns

PairSet<TKey, TValue>

A PairSet<TKey, TValue> containing the key-value pairs.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when source is null.

ToPairSet<TSource, TKey, TValue>(IEnumerable<TSource>, Func<TSource, int, TKey>, Func<TSource, int, TValue>)

Converts a sequence into a PairSet<TKey, TValue> using selector functions for keys and values.

public static PairSet<TKey, TValue> ToPairSet<TSource, TKey, TValue>(this IEnumerable<TSource> source, Func<TSource, int, TKey> keySelector, Func<TSource, int, TValue> valueSelector)

Parameters

source IEnumerable<TSource>

The source sequence.

keySelector Func<TSource, int, TKey>

The function to extract keys from elements.

valueSelector Func<TSource, int, TValue>

The function to extract values from elements.

Returns

PairSet<TKey, TValue>

A PairSet<TKey, TValue> containing the transformed key-value pairs.

Type Parameters

TSource

The type of elements in the source sequence.

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when source, keySelector, or valueSelector is null.

ToPair<TKey, TValue>(Tuple<TKey, TValue>)

Converts a tuple to a key-value pair.

public static KeyValuePair<TKey, TValue> ToPair<TKey, TValue>(this Tuple<TKey, TValue> pair)

Parameters

pair Tuple<TKey, TValue>

The tuple to convert.

Returns

KeyValuePair<TKey, TValue>

A key-value pair containing the tuple's items.

Type Parameters

TKey

The type of the key.

TValue

The type of the value.

Exceptions

ArgumentNullException

Thrown when pair is null.

ToPair<TKey, TValue>((TKey key, TValue value))

Converts a value tuple to a key-value pair.

public static KeyValuePair<TKey, TValue> ToPair<TKey, TValue>(this (TKey key, TValue value) _)

Parameters

_ (TKey key, TValue value)

The value tuple to convert.

Returns

KeyValuePair<TKey, TValue>

A KeyValuePair<TKey, TValue> containing the tuple's key and value.

Type Parameters

TKey

The type of the key.

TValue

The type of the value.

ToSet<T>(IEnumerable<T>)

Converts a sequence to a set using the default equality comparer.

public static ISet<T> ToSet<T>(this IEnumerable<T> values)

Parameters

values IEnumerable<T>

The sequence to convert.

Returns

ISet<T>

A set containing the unique elements from the sequence.

Type Parameters

T

The type of elements in the sequence.

ToTuple<TKey, TValue>(KeyValuePair<TKey, TValue>)

Converts a key-value pair to a tuple.

public static Tuple<TKey, TValue> ToTuple<TKey, TValue>(this KeyValuePair<TKey, TValue> pair)

Parameters

pair KeyValuePair<TKey, TValue>

The key-value pair to convert.

Returns

Tuple<TKey, TValue>

A tuple containing the key and value.

Type Parameters

TKey

The type of the key.

TValue

The type of the value.

TryAdd2<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)

Adds a key-value pair to a dictionary if the key does not already exist.

public static bool TryAdd2<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, TValue value)

Parameters

dict IDictionary<TKey, TValue>

The dictionary to add the pair to.

key TKey

The key to add.

value TValue

The value to add.

Returns

bool

True if the pair was added; false if the key already existed.

Type Parameters

TKey

The type of keys in the dictionary.

TValue

The type of values in the dictionary.

Exceptions

ArgumentNullException

Thrown when dict is null.

TryAdd<T>(ICollection<T>, IEnumerable<T>)

Adds a range of values to a collection, ensuring no duplicates if the collection already contains them.

public static void TryAdd<T>(this ICollection<T> collection, IEnumerable<T> values)

Parameters

collection ICollection<T>

The collection to add values to.

values IEnumerable<T>

The values to add.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when values is null.

TryAdd<T>(ICollection<T>, T)

Adds a value to a collection if it does not already exist.

public static bool TryAdd<T>(this ICollection<T> collection, T value)

Parameters

collection ICollection<T>

The collection to add the value to.

value T

The value to add.

Returns

bool

True if the value was added; false if it already existed.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when collection is null.

TryDequeue2<T>(SynchronizedQueue<T>)

Attempts to dequeue an item from a synchronized queue, returning null if the queue is empty (for value types).

public static T? TryDequeue2<T>(this SynchronizedQueue<T> queue) where T : struct

Parameters

queue SynchronizedQueue<T>

The synchronized queue to dequeue from.

Returns

T?

The dequeued item if available; otherwise, null.

Type Parameters

T

The type of elements in the queue, which must be a value type.

TryDequeue2<T>(Queue<T>)

Attempts to dequeue an item from a queue, returning null if the queue is empty (for value types).

public static T? TryDequeue2<T>(this Queue<T> queue) where T : struct

Parameters

queue Queue<T>

The queue to dequeue from.

Returns

T?

The dequeued item if available; otherwise, null.

Type Parameters

T

The type of elements in the queue, which must be a value type.

TryDequeue<T>(SynchronizedQueue<T>)

Attempts to dequeue an item from a synchronized queue, returning null if the queue is empty (for reference types).

public static T TryDequeue<T>(this SynchronizedQueue<T> queue) where T : class

Parameters

queue SynchronizedQueue<T>

The synchronized queue to dequeue from.

Returns

T

The dequeued item if available; otherwise, null.

Type Parameters

T

The type of elements in the queue, which must be a reference type.

TryDequeue<T>(Queue<T>)

Attempts to dequeue an item from a queue, returning null if the queue is empty (for reference types).

public static T TryDequeue<T>(this Queue<T> queue) where T : class

Parameters

queue Queue<T>

The queue to dequeue from.

Returns

T

The dequeued item if available; otherwise, null.

Type Parameters

T

The type of elements in the queue, which must be a reference type.

TryGetAndRemove2<TKey, TValue>(IDictionary<TKey, TValue>, TKey)

Attempts to retrieve a value from a dictionary by key and remove it, returning null if not found (for value types).

public static TValue? TryGetAndRemove2<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key) where TValue : struct

Parameters

dict IDictionary<TKey, TValue>

The dictionary to operate on.

key TKey

The key of the value to retrieve and remove.

Returns

TValue?

The value if found and removed; otherwise, null.

Type Parameters

TKey

The type of keys in the dictionary.

TValue

The type of values in the dictionary, which must be a value type.

TryGetAndRemove<TKey, TValue>(IDictionary<TKey, TValue>, TKey)

Attempts to retrieve a value from a dictionary by key and remove it, returning the default value if not found.

public static TValue TryGetAndRemove<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key)

Parameters

dict IDictionary<TKey, TValue>

The dictionary to operate on.

key TKey

The key of the value to retrieve and remove.

Returns

TValue

The value if found and removed; otherwise, the default value of TValue.

Type Parameters

TKey

The type of keys in the dictionary.

TValue

The type of values in the dictionary.

TryGetAndRemove<TKey, TValue>(IDictionary<TKey, TValue>, TKey, out TValue)

Attempts to retrieve a value from a dictionary by key and remove it, indicating success.

public static bool TryGetAndRemove<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key, out TValue value)

Parameters

dict IDictionary<TKey, TValue>

The dictionary to operate on.

key TKey

The key of the value to retrieve and remove.

value TValue

When this method returns, contains the value if found; otherwise, the default value.

Returns

bool

True if the value was found and removed; otherwise, false.

Type Parameters

TKey

The type of keys in the dictionary.

TValue

The type of values in the dictionary.

TryGetKey2<TKey, TValue>(PairSet<TKey, TValue>, TValue)

Attempts to retrieve a key from a pair set by value, returning null if not found (for value-type keys).

public static TKey? TryGetKey2<TKey, TValue>(this PairSet<TKey, TValue> pairSet, TValue value) where TKey : struct

Parameters

pairSet PairSet<TKey, TValue>

The pair set to query.

value TValue

The value to look up.

Returns

TKey?

The key if found; otherwise, null.

Type Parameters

TKey

The type of keys, which must be a value type.

TValue

The type of values.

TryGetKey<TKey, TValue>(PairSet<TKey, TValue>, TValue)

Attempts to retrieve a key from a pair set by value, returning the default value if not found.

public static TKey TryGetKey<TKey, TValue>(this PairSet<TKey, TValue> pairSet, TValue value)

Parameters

pairSet PairSet<TKey, TValue>

The pair set to query.

value TValue

The value to look up.

Returns

TKey

The key if found; otherwise, the default value of TKey.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

TryGetValue2<TKey, TValue>(IDictionary<TKey, TValue>, TKey)

Attempts to retrieve a value from a dictionary by key, returning null if not found (for value types).

public static TValue? TryGetValue2<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key) where TValue : struct

Parameters

dict IDictionary<TKey, TValue>

The dictionary to query.

key TKey

The key to look up.

Returns

TValue?

The value if found; otherwise, null.

Type Parameters

TKey

The type of keys.

TValue

The type of values, which must be a value type.

Exceptions

ArgumentNullException

Thrown when dict is null.

TryGetValue<TKey, TValue>(IDictionary<TKey, TValue>, TKey)

Attempts to retrieve a value from a dictionary by key, returning the default value if not found.

public static TValue TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dict, TKey key)

Parameters

dict IDictionary<TKey, TValue>

The dictionary to query.

key TKey

The key to look up.

Returns

TValue

The value if found; otherwise, the default value of TValue.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when dict is null.

TryPeek2<T>(SynchronizedQueue<T>)

Attempts to peek at the next item in a synchronized queue, returning null if the queue is empty (for value types).

public static T? TryPeek2<T>(this SynchronizedQueue<T> queue) where T : struct

Parameters

queue SynchronizedQueue<T>

The synchronized queue to peek into.

Returns

T?

The next item if available; otherwise, null.

Type Parameters

T

The type of elements in the queue, which must be a value type.

TryPeek2<T>(Queue<T>)

Attempts to peek at the next item in a queue, returning null if the queue is empty (for value types).

public static T? TryPeek2<T>(this Queue<T> queue) where T : struct

Parameters

queue Queue<T>

The queue to peek into.

Returns

T?

The next item if available; otherwise, null.

Type Parameters

T

The type of elements in the queue, which must be a value type.

TryPeek<T>(SynchronizedQueue<T>)

Attempts to peek at the next item in a synchronized queue, returning null if the queue is empty (for reference types).

public static T TryPeek<T>(this SynchronizedQueue<T> queue) where T : class

Parameters

queue SynchronizedQueue<T>

The synchronized queue to peek into.

Returns

T

The next item if available; otherwise, null.

Type Parameters

T

The type of elements in the queue, which must be a reference type.

TryPeek<T>(Queue<T>)

Attempts to peek at the next item in a queue, returning null if the queue is empty (for reference types).

public static T TryPeek<T>(this Queue<T> queue) where T : class

Parameters

queue Queue<T>

The queue to peek into.

Returns

T

The next item if available; otherwise, null.

Type Parameters

T

The type of elements in the queue, which must be a reference type.

TypedAs<TKey, TValue>(IDictionary)

Converts a non-generic dictionary to a strongly-typed dictionary.

public static IDictionary<TKey, TValue> TypedAs<TKey, TValue>(this IDictionary dictionary)

Parameters

dictionary IDictionary

The non-generic dictionary.

Returns

IDictionary<TKey, TValue>

A strongly-typed dictionary with the same key-value pairs.

Type Parameters

TKey

The type of keys.

TValue

The type of values.

Exceptions

ArgumentNullException

Thrown when dictionary is null.

WhereWithPrevious<TSource>(IEnumerable<TSource>, Func<TSource, TSource, bool>)

Filters a sequence based on a predicate that compares each element with its previous element.

public static IEnumerable<TSource> WhereWithPrevious<TSource>(this IEnumerable<TSource> source, Func<TSource, TSource, bool> predicate)

Parameters

source IEnumerable<TSource>

The sequence to filter.

predicate Func<TSource, TSource, bool>

The function to test each element against its previous element.

Returns

IEnumerable<TSource>

An enumerable of elements where the predicate returns true when compared to the previous element.

Type Parameters

TSource

The type of elements in the sequence.

Exceptions

ArgumentNullException

Thrown when source or predicate is null.