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
Exceptions
- ArgumentNullException
- Thrown when - arrayor- bitsis 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
- sourceICollection<T>
- The collection to add items to. 
- itemsIEnumerable<T>
- The items to add. 
Type Parameters
- T
- The type of elements in the collection. 
Exceptions
- ArgumentNullException
- Thrown when - sourceor- itemsis 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
- dictIDictionary<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
- valuesIEnumerable<T>
- The sequence to append to. 
- valueT
- 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
- sourceIEnumerable<T>
- The sequence to batch. 
- sizeint
- The size of each batch. 
Returns
- IEnumerable<T[]>
- An enumerable of arrays, each containing up to - sizeelements.
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
- sourceIEnumerable<TSource>
- The sequence to batch. 
- sizeint
- The maximum size of each batch. 
- resultSelectorFunc<IEnumerable<TSource>, TResult>
- The function to transform each batch into a result. 
- needStopFunc<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
- sourceINotifyList<T>
- The source list that notifies changes. 
- destinationIList<T>
- The destination list to synchronize with. 
Type Parameters
- T
- The type of elements in the lists. 
Exceptions
- ArgumentNullException
- Thrown when - sourceor- destinationis 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
- firstT
- The first collection. 
- secondT
- 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
- itemsICollection<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
- sourceIEnumerable<KeyValuePair<TKey, TValue>>
- The sequence of key-value pairs. 
- destinationIDictionary<TKey, TValue>
- The dictionary to copy to. 
Type Parameters
- TKey
- The type of keys. 
- TValue
- The type of values. 
Exceptions
- ArgumentNullException
- Thrown when - sourceor- destinationis null.
Count2(IEnumerable)
Counts the number of elements in a non-generic sequence.
public static int Count2(this IEnumerable source)Parameters
- sourceIEnumerable
- 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
- sourceT[]
- The first sequence. 
- targetT[]
- The second sequence. 
- thresholdint
- 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 - sourceor- targetis 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
- listSynchronizedLinkedList<T>
- The synchronized linked list to query. 
- indexint
- 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 - listis 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
- sourceIEnumerable<T>
- The sequence to query. 
- indexint
- 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
- listLinkedList<T>
- The linked list to query. 
- indexint
- 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 - listis 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
- listSynchronizedLinkedList<T>
- The synchronized linked list to query. 
- indexint
- 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 - listis 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
- sourceIEnumerable<T>
- The sequence to query. 
- indexint
- 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
- listLinkedList<T>
- The linked list to query. 
- indexint
- 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 - listis 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 : structParameters
- sourceIEnumerable<T>
- The sequence to query. 
- indexint
- 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 - sourceis null.
- ArgumentOutOfRangeException
- Thrown when - indexis 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 : structParameters
- sourceIEnumerable<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 - sourceis 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
- sourceIEnumerable<T>
- The sequence to query. 
- alternateT
- The value to return if the sequence is empty. 
Returns
- T
- The first element, or - alternateif 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
- sourceIEnumerable<T>
- The sequence to iterate over. 
- actionAction<T>
- The action to apply to each element. 
Type Parameters
- T
- The type of elements in the sequence. 
Exceptions
- ArgumentNullException
- Thrown when - sourceor- actionis null.
FromBits(bool[])
Converts an array of bits to an integer starting from the beginning.
public static int FromBits(this bool[] bits)Parameters
- bitsbool[]
- 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
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
- bitsbool[]
- 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
Returns
- long
- The long integer value represented by the bits. 
Exceptions
- ArgumentNullException
- Thrown when - bitsis null.
- ArgumentOutOfRangeException
- Thrown when - startBitis 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
- dictIDictionary<TKey, TValue>
- The dictionary to operate on. 
- keyTKey
- 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
- collectionIEnumerable<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 - collectionis 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
- dictionarySynchronizedDictionary<TKey, TValue>
- The synchronized dictionary to query. 
- valueTValue
- 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
- dictionaryIDictionary<TKey, TValue>
- The dictionary to query. 
- valueTValue
- 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 : classParameters
- itemsIEnumerable<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
- sourceIEnumerable<T>
- The sequence to search. 
- predicateFunc<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
- sourceIEnumerable<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
- sourceICollection<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
- sourceIEnumerable<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
- sourceIEnumerable<T>
- The sequence to check. 
- predicateFunc<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
- sourceT[]
- 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 - sourceis 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 : structParameters
- sourceIEnumerable<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 - sourceis 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
- valuesIEnumerable<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
- valuesIEnumerable<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
- collectionIEnumerable<T>
- The collection to sort. 
- comparisonComparison<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
- keysIEnumerable<TKey>
- The sequence of keys. 
- selectorFunc<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 - keysor- selectoris 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
- sourceICollection<T>
- The collection to remove items from. 
- itemsIEnumerable<T>
- The items to remove. 
Type Parameters
- T
- The type of elements in the collection. 
Exceptions
- ArgumentNullException
- Thrown when - itemsis 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
- listIList<T>
- The list to remove items from. 
- filterFunc<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
- collectionICollection<T>
- The collection to remove items from. 
- filterFunc<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
- dictionaryIDictionary<TKey, TaskCompletionSource<TValue>>
- The dictionary mapping keys to task completion sources. 
- syncAsyncReaderWriterLock
- The reader-writer lock for synchronization. 
- keyTKey
- The key to add or retrieve. 
- handlerFunc<TKey, CancellationToken, Task<TValue>>
- The asynchronous function to generate a value if the key is new. 
- cancellationTokenCancellationToken
- 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- handleris 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
- dictionaryIDictionary<TKey, TaskCompletionSource<TValue>>
- The dictionary mapping keys to task completion sources. 
- keyTKey
- The key to add or retrieve. 
- handlerFunc<TKey, CancellationToken, Task<TValue>>
- The asynchronous function to generate a value if the key is new. 
- cancellationTokenCancellationToken
- 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 - dictionaryor- handleris 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
- dictionaryIDictionary<TKey, TValue>
- The dictionary to operate on. 
- syncAsyncReaderWriterLock
- The reader-writer lock for synchronization. 
- keyTKey
- The key to add or retrieve. 
- handlerFunc<TKey, CancellationToken, Task<TValue>>
- The asynchronous function to generate a value if the key is new. 
- cancellationTokenCancellationToken
- 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- handleris 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
- dictionaryIDictionary<TKey, TValue>
- The dictionary to operate on. 
- keyTKey
- 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
- dictionaryIDictionary<TKey, TValue>
- The dictionary to operate on. 
- keyTKey
- The key to add or retrieve. 
- isNewbool
- 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
- dictionaryIDictionary<TKey, TValue>
- The dictionary to operate on. 
- keyTKey
- The key to add or retrieve. 
- handlerFunc<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 - dictionaryor- handleris 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
- dictionaryIDictionary<TKey, TValue>
- The dictionary to operate on. 
- keyTKey
- The key to add or retrieve. 
- handlerFunc<TKey, TValue>
- The function to generate a value if the key is new. 
- isNewbool
- 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 - dictionaryor- handleris 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
- enumerationIEnumerable<T>
- The source sequence of elements. 
- funcFunc<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
- valuesIEnumerable<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
- firstIEnumerable<T>
- The first sequence to compare. 
- secondIEnumerable<T>
- The second sequence to compare. 
- comparerFunc<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
- sourceIEnumerable<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, ISynchronizedCollectionParameters
- collectionTCollection
- The synchronized collection to operate on. 
- actionAction<TCollection>
- The action to execute. 
Type Parameters
- TCollection
- The type of the synchronized collection. 
Exceptions
- ArgumentNullException
- Thrown when - collectionor- actionis 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, ISynchronizedCollectionParameters
- collectionTCollection
- The synchronized collection to operate on. 
- funcFunc<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 - collectionor- funcis null.
Sync<T>(HashSet<T>)
Converts a hash set to a synchronized set.
public static SynchronizedSet<T> Sync<T>(this HashSet<T> list)Parameters
- listHashSet<T>
- The hash set to synchronize. 
Returns
- SynchronizedSet<T>
- A SynchronizedSet<T> containing the set's elements, or null if - listis 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
- listIList<T>
- The list to synchronize. 
Returns
- SynchronizedList<T>
- A SynchronizedList<T> containing the list's elements, or null if - listis 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
- dictIDictionary<TKey, TValue>
- The dictionary to synchronize. 
Returns
- SynchronizedDictionary<TKey, TValue>
- A SynchronizedDictionary<TKey, TValue> containing the dictionary's elements, or null if - dictis 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
- valuedouble
- 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
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
- valuedouble
- The double value to convert. 
- startBitint
- The starting bit position (0-based). 
- bitCountint
- 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
- valueint
- 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
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
- valueint
- The int value to convert. 
- startBitint
- The starting bit position (0-based). 
- bitCountint
- The number of bits to return. 
Returns
- bool[]
- An array of bits representing the specified range. 
Exceptions
- ArgumentOutOfRangeException
- Thrown when - startBitis outside [0, 31] or- bitCountis 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
- valuelong
- 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
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
- valuelong
- The long value to convert. 
- startBitint
- The starting bit position (0-based). 
- bitCountint
- 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
- valuefloat
- 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
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
- valuefloat
- The float value to convert. 
- startBitint
- The starting bit position (0-based). 
- bitCountint
- 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
- comparerComparison<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
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
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
- sourceIEnumerable<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
- sourceIEnumerable<KeyValuePair<TKey, TValue>>
- The sequence of key-value pairs. 
- comparerIEqualityComparer<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
- groupingIEnumerable<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 - groupingis 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
- sourceIEnumerable<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
- sourceIEnumerable<Tuple<TKey, TValue>>
- The sequence of tuples. 
- comparerIEqualityComparer<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
- sourceIEnumerable<TSource>
- The source sequence. 
- keySelectorFunc<TSource, int, TKey>
- The function to extract keys from elements. 
- valueSelectorFunc<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- valueSelectoris 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
- valuesIEnumerable<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
- valuesIEnumerable<T>
- The enumerable to convert. 
- countint
- 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
- comparerComparison<T>
- The comparison delegate. 
Returns
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
- valuesIEnumerable<string>
- The sequence of strings to convert. 
Returns
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
- sourceIEnumerable<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
- sourceIEnumerable<KeyValuePair<TKey, TValue>>
- The sequence of key-value pairs. 
- comparerIEqualityComparer<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 - sourceis 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
- sourceIEnumerable<TSource>
- The source sequence. 
- keySelectorFunc<TSource, int, TKey>
- The function to extract keys from elements. 
- valueSelectorFunc<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- valueSelectoris 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
- pairTuple<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 - pairis 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
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
- valuesIEnumerable<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
- pairKeyValuePair<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
- dictIDictionary<TKey, TValue>
- The dictionary to add the pair to. 
- keyTKey
- The key to add. 
- valueTValue
- 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 - dictis 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
- collectionICollection<T>
- The collection to add values to. 
- valuesIEnumerable<T>
- The values to add. 
Type Parameters
- T
- The type of elements in the collection. 
Exceptions
- ArgumentNullException
- Thrown when - valuesis 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
- collectionICollection<T>
- The collection to add the value to. 
- valueT
- 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 - collectionis 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 : structParameters
- queueSynchronizedQueue<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 : structParameters
- queueQueue<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 : classParameters
- queueSynchronizedQueue<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 : classParameters
- queueQueue<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 : structParameters
- dictIDictionary<TKey, TValue>
- The dictionary to operate on. 
- keyTKey
- 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
- dictIDictionary<TKey, TValue>
- The dictionary to operate on. 
- keyTKey
- 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
- dictIDictionary<TKey, TValue>
- The dictionary to operate on. 
- keyTKey
- The key of the value to retrieve and remove. 
- valueTValue
- 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 : structParameters
- pairSetPairSet<TKey, TValue>
- The pair set to query. 
- valueTValue
- 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
- pairSetPairSet<TKey, TValue>
- The pair set to query. 
- valueTValue
- 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 : structParameters
- dictIDictionary<TKey, TValue>
- The dictionary to query. 
- keyTKey
- 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 - dictis 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
- dictIDictionary<TKey, TValue>
- The dictionary to query. 
- keyTKey
- 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 - dictis 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 : structParameters
- queueSynchronizedQueue<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 : structParameters
- queueQueue<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 : classParameters
- queueSynchronizedQueue<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 : classParameters
- queueQueue<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
- dictionaryIDictionary
- 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 - dictionaryis 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
- sourceIEnumerable<TSource>
- The sequence to filter. 
- predicateFunc<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 - sourceor- predicateis null.