CollectionHelper

Ecng.Collections

Provides extension methods and helper utilities for working with collections.

Métodos

AddRange
public static void AddRange(BitArray array, bool[] bits)
CollectionHelper.AddRange(array, bits)

Adds a range of bits to an existing BitArray.

array
The BitArray to extend.
bits
The bits to add.
AddRange``1
public static void AddRange<T>(ICollection<T> source, IEnumerable<T> items)
CollectionHelper.AddRange(source, items)

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

source
The collection to add items to.
items
The items to add.
Add``2
public static void Add<TKey, TValue>(IDictionary<TKey, TValue> dict, ValueTuple<TKey, TValue> tuple)
CollectionHelper.Add(dict, tuple)

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

dict
The dictionary to add to.
tuple
The value tuple to add.
AsDuckTypedCollection``2
public static ICollection<TTarget> AsDuckTypedCollection<TSource, TTarget>(ICollection<TSource> source, Func<TSource, TTarget> sourceToTarget, Func<TTarget, TSource> targetToSource)
result = CollectionHelper.AsDuckTypedCollection(source, sourceToTarget, targetToSource)

Creates a collection adapter that converts between ICollection and ICollection using duck typing.

source
The source collection to wrap.
sourceToTarget
Function to convert from source type to target type.
targetToSource
Function to convert from target type to source type.

Devuelve: An ICollection adapter around the source collection.

Bind``1
public static void Bind<T>(INotifyList<T> source, IList<T> destination)
CollectionHelper.Bind(source, destination)

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

source
The source list that notifies changes.
destination
The destination list to synchronize with.
ConcatEx``2
public static T ConcatEx<T, TItem>(T first, T second)
result = CollectionHelper.ConcatEx(first, second)

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

first
The first collection.
second
The second collection.

Devuelve: A new collection containing all elements from both input collections.

CopyAndClear``1
public static T[] CopyAndClear<T>(ICollection<T> items)
result = CollectionHelper.CopyAndClear(items)

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

items
The collection to copy and clear.

Devuelve: An array containing the copied items.

CopyTo``2
public static void CopyTo<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> source, IDictionary<TKey, TValue> destination)
CollectionHelper.CopyTo(source, destination)

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

source
The sequence of key-value pairs.
destination
The dictionary to copy to.
Count2
public static int Count2(IEnumerable source)
result = CollectionHelper.Count2(source)

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

source
The sequence to count.

Devuelve: The number of elements in the sequence.

DamerauLevenshteinDistance``1
public static int DamerauLevenshteinDistance<T>(T[] source, T[] target, int threshold)
result = CollectionHelper.DamerauLevenshteinDistance(source, target, threshold)

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

source
The first sequence.
target
The second sequence.
threshold
The maximum distance to compute; returns int.MaxValue if exceeded.

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

ElementAtFromEndOrDefault``1
public static T ElementAtFromEndOrDefault<T>(IEnumerable<T> source, int index)
result = CollectionHelper.ElementAtFromEndOrDefault(source, index)

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

source
The sequence to query.
index
The zero-based index from the end (0 is the last element).

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

ElementAtFromEndOrDefault``1
public static T ElementAtFromEndOrDefault<T>(SynchronizedLinkedList<T> list, int index)
result = CollectionHelper.ElementAtFromEndOrDefault(list, index)

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

list
The synchronized linked list to query.
index
The zero-based index from the end (0 is the last element).

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

ElementAtFromEndOrDefault``1
public static T ElementAtFromEndOrDefault<T>(LinkedList<T> list, int index)
result = CollectionHelper.ElementAtFromEndOrDefault(list, index)

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

list
The linked list to query.
index
The zero-based index from the end (0 is the last element).

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

ElementAtFromEnd``1
public static T ElementAtFromEnd<T>(SynchronizedLinkedList<T> list, int index)
result = CollectionHelper.ElementAtFromEnd(list, index)

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

list
The synchronized linked list to query.
index
The zero-based index from the end (0 is the last element).

Devuelve: The element at the specified position from the end.

ElementAtFromEnd``1
public static T ElementAtFromEnd<T>(LinkedList<T> list, int index)
result = CollectionHelper.ElementAtFromEnd(list, index)

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

list
The linked list to query.
index
The zero-based index from the end (0 is the last element).

Devuelve: The element at the specified position from the end.

ElementAtFromEnd``1
public static T ElementAtFromEnd<T>(IEnumerable<T> source, int index)
result = CollectionHelper.ElementAtFromEnd(source, index)

Retrieves an element from a sequence counting from the end.

source
The sequence to query.
index
The zero-based index from the end (0 is the last element).

Devuelve: The element at the specified position from the end.

ElementAtOr``1
public static T? ElementAtOr<T>(IEnumerable<T> source, int index)
result = CollectionHelper.ElementAtOr(source, index)

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

source
The sequence to query.
index
The zero-based index of the element to retrieve.

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

FirstOr``1
public static T FirstOr<T>(IEnumerable<T> source, T alternate)
result = CollectionHelper.FirstOr(source, alternate)

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

source
The sequence to query.
alternate
The value to return if the sequence is empty.

Devuelve: The first element, or if the sequence is empty.

FirstOr``1
public static T? FirstOr<T>(IEnumerable<T> source)
result = CollectionHelper.FirstOr(source)

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

source
The sequence to query.

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

ForEach``1
public static void ForEach<T>(IEnumerable<T> source, Action<T> action)
CollectionHelper.ForEach(source, action)

Applies an action to each element in a sequence.

source
The sequence to iterate over.
action
The action to apply to each element.
FromBits
public static int FromBits(bool[] bits, int startBit)
result = CollectionHelper.FromBits(bits, startBit)

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

bits
The array of bits to convert.
startBit
The starting bit position (0-based).

Devuelve: The integer value represented by the bits.

FromBits
public static int FromBits(bool[] bits)
result = CollectionHelper.FromBits(bits)

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

bits
The array of bits to convert.

Devuelve: The integer value represented by the bits.

FromBits2
public static long FromBits2(bool[] bits)
result = CollectionHelper.FromBits2(bits)

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

bits
The array of bits to convert.

Devuelve: The long integer value represented by the bits.

FromBits2
public static long FromBits2(bool[] bits, int startBit)
result = CollectionHelper.FromBits2(bits, startBit)

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

bits
The array of bits to convert.
startBit
The starting bit position (0-based).

Devuelve: The long integer value represented by the bits.

GetAndRemove``2
public static TValue GetAndRemove<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key)
result = CollectionHelper.GetAndRemove(dict, key)

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

dict
The dictionary to operate on.
key
The key of the value to retrieve and remove.

Devuelve: The value associated with the key.

GetHashCodeEx``1
public static int GetHashCodeEx<T>(IEnumerable<T> collection)
result = CollectionHelper.GetHashCodeEx(collection)

Computes a hash code for a sequence of elements.

collection
The sequence to compute the hash code for.

Devuelve: A hash code for the sequence.

GetIndexFromEnd``1
private static int GetIndexFromEnd<T>(IEnumerable<T> source, int index)
result = CollectionHelper.GetIndexFromEnd(source, index)

Calculates the index from the start of a sequence given an index from the end.

source
The sequence to query.
index
The zero-based index from the end.

Devuelve: The zero-based index from the start.

GetKeys``2
public static IEnumerable<TKey> GetKeys<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TValue value)
result = CollectionHelper.GetKeys(dictionary, value)

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

dictionary
The dictionary to query.
value
The value to match.

Devuelve: An enumerable of keys associated with the specified value.

GetKeys``2
public static IEnumerable<TKey> GetKeys<TKey, TValue>(SynchronizedDictionary<TKey, TValue> dictionary, TValue value)
result = CollectionHelper.GetKeys(dictionary, value)

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

dictionary
The synchronized dictionary to query.
value
The value to match.

Devuelve: An enumerable of keys associated with the specified value.

HasNullItem``1
public static bool HasNullItem<T>(IEnumerable<T> items)
result = CollectionHelper.HasNullItem(items)

Determines whether a sequence contains any null items.

items
The sequence to check.

Devuelve: True if the sequence contains null; otherwise, false.

IndexOf``1
public static int IndexOf<T>(IEnumerable<T> source, Func<T, bool> predicate)
result = CollectionHelper.IndexOf(source, predicate)

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

source
The sequence to search.
predicate
The condition to test each element against.

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

IsEmpty
public static bool IsEmpty(IEnumerable<char> source)
result = CollectionHelper.IsEmpty(source)

Determines whether a character sequence is null or empty.

source
The sequence of characters to check.

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

IsEmpty``1
public static bool IsEmpty<T>(T[] source)
result = CollectionHelper.IsEmpty(source)

Determines whether an array is empty.

source
The array to check.

Devuelve: True if the array is empty; otherwise, false.

IsEmpty``1
public static bool IsEmpty<T>(ICollection<T> source)
result = CollectionHelper.IsEmpty(source)

Determines whether a collection is empty.

source
The collection to check.

Devuelve: True if the collection is empty; otherwise, false.

IsEmpty``1
public static bool IsEmpty<T>(IEnumerable<T> source)
result = CollectionHelper.IsEmpty(source)

Determines whether a sequence is empty.

source
The sequence to check.

Devuelve: True if the sequence is empty; otherwise, false.

IsEmpty``1
public static bool IsEmpty<T>(ICircularBuffer<T> buffer)
result = CollectionHelper.IsEmpty(buffer)

Boolean indicating if the ICircularBuffer is empty.

buffer
ICircularBuffer

Devuelve: True if the buffer is empty; otherwise, false.

IsEmpty``1
public static bool IsEmpty<T>(IEnumerable<T> source, Func<T, bool> predicate)
result = CollectionHelper.IsEmpty(source, predicate)

Determines whether a sequence is empty based on a predicate.

source
The sequence to check.
predicate
The condition to test each element against.

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

IsFull``1
public static bool IsFull<T>(ICircularBuffer<T> buffer)
result = CollectionHelper.IsFull(buffer)

Boolean indicating if ICircularBuffer is at full capacity. Adding more elements when the buffer is full will cause elements to be removed from the other end of the buffer.

buffer
ICircularBuffer

Devuelve: True if the buffer is full; otherwise, false.

LastOr``1
public static T? LastOr<T>(IEnumerable<T> source)
result = CollectionHelper.LastOr(source)

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

source
The sequence to query.

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

OrderByDescending``1
public static IEnumerable<T> OrderByDescending<T>(IEnumerable<T> values)
result = CollectionHelper.OrderByDescending(values)

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

values
The sequence to order.

Devuelve: An ordered sequence of the input values in descending order.

OrderBy``1
public static IEnumerable<T> OrderBy<T>(IEnumerable<T> values)
result = CollectionHelper.OrderBy(values)

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

values
The sequence to order.

Devuelve: An ordered sequence of the input values.

OrderBy``1
public static IOrderedEnumerable<T> OrderBy<T>(IEnumerable<T> collection, Comparison<T> comparison)
result = CollectionHelper.OrderBy(collection, comparison)

Sorts a collection using a Comparison delegate.

collection
The collection to sort.
comparison
The comparison delegate to determine order.

Devuelve: An ordered enumerable of the collection.

Permutations``2
public static IEnumerable<TValue[]> Permutations<TKey, TValue>(IEnumerable<TKey> keys, Func<TKey, IEnumerable<TValue>> selector)
result = CollectionHelper.Permutations(keys, selector)

Generates all possible permutations of values selected from keys.

keys
The sequence of keys.
selector
The function to select possible values for each key.

Devuelve: An enumerable of arrays representing all permutations.

RemoveRange``1
public static void RemoveRange<T>(ICollection<T> source, IEnumerable<T> items)
CollectionHelper.RemoveRange(source, items)

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

source
The collection to remove items from.
items
The items to remove.
RemoveWhere2``1
public static int RemoveWhere2<T>(IList<T> list, Func<T, bool> filter)
result = CollectionHelper.RemoveWhere2(list, filter)

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

list
The list to remove items from.
filter
The condition to identify items to remove.

Devuelve: The number of items removed.

RemoveWhere``1
public static IEnumerable<T> RemoveWhere<T>(ICollection<T> collection, Func<T, bool> filter)
result = CollectionHelper.RemoveWhere(collection, filter)

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

collection
The collection to remove items from.
filter
The condition to identify items to remove.

Devuelve: An enumerable of the removed items.

SafeAddAsyncEx``2
public static Task<ValueTuple<TValue, bool>> SafeAddAsyncEx<TKey, TValue>(IDictionary<TKey, TValue> dictionary, AsyncReaderWriterLock sync, TKey key, Func<TKey, CancellationToken, Task<TValue>> handler, CancellationToken cancellationToken)
result = CollectionHelper.SafeAddAsyncEx(dictionary, sync, key, handler, 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.

dictionary
The dictionary to operate on.
sync
The reader-writer lock for synchronization.
key
The key to add or retrieve.
handler
The asynchronous function to generate a value if the key is new.
cancellationToken
The cancellation token to cancel the operation.

Devuelve: The existing or newly created value.

SafeAddAsync``2
public static Task<TValue> SafeAddAsync<TKey, TValue>(IDictionary<TKey, TaskCompletionSource<TValue>> dictionary, TKey key, Func<TKey, CancellationToken, Task<TValue>> handler, CancellationToken cancellationToken)
result = CollectionHelper.SafeAddAsync(dictionary, key, handler, cancellationToken)

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

dictionary
The dictionary mapping keys to task completion sources.
key
The key to add or retrieve.
handler
The asynchronous function to generate a value if the key is new.
cancellationToken
The cancellation token to cancel the operation.

Devuelve: The task representing the value for the key.

SafeAddAsync``2
public static Task<TValue> SafeAddAsync<TKey, TValue>(IDictionary<TKey, TaskCompletionSource<TValue>> dictionary, AsyncReaderWriterLock sync, TKey key, Func<TKey, CancellationToken, Task<TValue>> handler, CancellationToken cancellationToken)
result = CollectionHelper.SafeAddAsync(dictionary, sync, key, handler, 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.

dictionary
The dictionary mapping keys to task completion sources.
sync
The reader-writer lock for synchronization.
key
The key to add or retrieve.
handler
The asynchronous function to generate a value if the key is new.
cancellationToken
The cancellation token to cancel the operation.

Devuelve: The task representing the value for the key.

SafeAddAsync``2
public static Task<TValue> SafeAddAsync<TKey, TValue>(IDictionary<TKey, TValue> dictionary, AsyncReaderWriterLock sync, TKey key, Func<TKey, CancellationToken, Task<TValue>> handler, CancellationToken cancellationToken)
result = CollectionHelper.SafeAddAsync(dictionary, sync, key, handler, 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.

dictionary
The dictionary to operate on.
sync
The reader-writer lock for synchronization.
key
The key to add or retrieve.
handler
The asynchronous function to generate a value if the key is new.
cancellationToken
The cancellation token to cancel the operation.

Devuelve: The existing or newly created value.

SafeAdd``2
public static TValue SafeAdd<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key)
result = CollectionHelper.SafeAdd(dictionary, key)

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

dictionary
The dictionary to operate on.
key
The key to add or retrieve.

Devuelve: The existing or newly created value.

SafeAdd``2
public static TValue SafeAdd<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> handler, bool isNew)
result = CollectionHelper.SafeAdd(dictionary, key, handler, isNew)

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

dictionary
The dictionary to operate on.
key
The key to add or retrieve.
handler
The function to generate a value if the key is new.
isNew
When this method returns, indicates whether the key was newly added.

Devuelve: The existing or newly created value.

SafeAdd``2
public static TValue SafeAdd<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> handler)
result = CollectionHelper.SafeAdd(dictionary, key, handler)

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

dictionary
The dictionary to operate on.
key
The key to add or retrieve.
handler
The function to generate a value if the key is new.

Devuelve: The existing or newly created value.

SafeAdd``2
public static TValue SafeAdd<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key, bool isNew)
result = CollectionHelper.SafeAdd(dictionary, key, isNew)

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

dictionary
The dictionary to operate on.
key
The key to add or retrieve.
isNew
When this method returns, indicates whether the key was newly added.

Devuelve: The existing or newly created value.

SelectManyAsync``2
public static Task<IEnumerable<T1>> SelectManyAsync<T, T1>(IEnumerable<T> enumeration, Func<T, Task<IEnumerable<T1>>> func)
result = CollectionHelper.SelectManyAsync(enumeration, func)

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

enumeration
The source sequence of elements.
func
The asynchronous function to transform each element into a sequence.

Devuelve: A task that resolves to a flattened sequence of results.

SelectMany``1
public static IEnumerable<T> SelectMany<T>(IEnumerable<IEnumerable<T>> values)
result = CollectionHelper.SelectMany(values)

Flattens a sequence of sequences into a single sequence.

values
The sequence of sequences to flatten.

Devuelve: A single sequence containing all elements from the input sequences.

SequenceEqual``1
public static bool SequenceEqual<T>(IEnumerable<T> first, IEnumerable<T> second, Func<T, T, bool> comparer)
result = CollectionHelper.SequenceEqual(first, second, comparer)

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

first
The first sequence to compare.
second
The second sequence to compare.
comparer
The function to determine equality between elements.

Devuelve: True if the sequences are equal; otherwise, false.

SingleWhenOnly``1
public static T SingleWhenOnly<T>(IEnumerable<T> source)
result = CollectionHelper.SingleWhenOnly(source)

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

source
The sequence to query.

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

SyncDo``1
public static void SyncDo<TCollection>(TCollection collection, Action<TCollection> action)
CollectionHelper.SyncDo(collection, action)

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

collection
The synchronized collection to operate on.
action
The action to execute.
SyncGet``2
public static TResult SyncGet<TCollection, TResult>(TCollection collection, Func<TCollection, TResult> func)
result = CollectionHelper.SyncGet(collection, func)

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

collection
The synchronized collection to operate on.
func
The function to execute.

Devuelve: The result of the function.

Sync``1
public static SynchronizedList<T> Sync<T>(IList<T> list)
result = CollectionHelper.Sync(list)

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

list
The list to synchronize.

Devuelve: A SynchronizedList containing the list's elements, or null if is null.

Sync``1
public static SynchronizedSet<T> Sync<T>(HashSet<T> list)
result = CollectionHelper.Sync(list)

Converts a hash set to a synchronized set.

list
The hash set to synchronize.

Devuelve: A SynchronizedSet containing the set's elements, or null if is null.

Sync``2
public static SynchronizedDictionary<TKey, TValue> Sync<TKey, TValue>(IDictionary<TKey, TValue> dict)
result = CollectionHelper.Sync(dict)

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

dict
The dictionary to synchronize.

Devuelve: A SynchronizedDictionary containing the dictionary's elements, or null if is null.

ToBits
public static bool[] ToBits(double value, int startBit, int bitCount)
result = CollectionHelper.ToBits(value, startBit, bitCount)

Converts a double value to a specified range of bits.

value
The double value to convert.
startBit
The starting bit position (0-based).
bitCount
The number of bits to return.

Devuelve: An array of bits representing the specified range.

ToBits
public static bool[] ToBits(double value, int count)
result = CollectionHelper.ToBits(value, count)

Converts a double value to a specified number of bits.

value
The double value to convert.
count
The number of bits to return.

Devuelve: An array of bits representing the value.

ToBits
public static bool[] ToBits(double value)
result = CollectionHelper.ToBits(value)

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

value
The double value to convert.

Devuelve: An array of 64 bits representing the value.

ToBits
public static bool[] ToBits(float value, int bitCount)
result = CollectionHelper.ToBits(value, bitCount)

Converts a float value to a specified number of bits.

value
The float value to convert.
bitCount
The number of bits to return.

Devuelve: An array of bits representing the value.

ToBits
public static bool[] ToBits(float value, int startBit, int bitCount)
result = CollectionHelper.ToBits(value, startBit, bitCount)

Converts a float value to a specified range of bits.

value
The float value to convert.
startBit
The starting bit position (0-based).
bitCount
The number of bits to return.

Devuelve: An array of bits representing the specified range.

ToBits
public static bool[] ToBits(long value, int bitCount)
result = CollectionHelper.ToBits(value, bitCount)

Converts a long value to a specified number of bits.

value
The long value to convert.
bitCount
The number of bits to return.

Devuelve: An array of bits representing the value.

ToBits
public static bool[] ToBits(long value, int startBit, int bitCount)
result = CollectionHelper.ToBits(value, startBit, bitCount)

Converts a long value to a specified range of bits.

value
The long value to convert.
startBit
The starting bit position (0-based).
bitCount
The number of bits to return.

Devuelve: An array of bits representing the specified range.

ToBits
public static bool[] ToBits(int value)
result = CollectionHelper.ToBits(value)

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

value
The int value to convert.

Devuelve: An array of 32 bits representing the value.

ToBits
public static bool[] ToBits(int value, int bitCount)
result = CollectionHelper.ToBits(value, bitCount)

Converts an int value to a specified number of bits.

value
The int value to convert.
bitCount
The number of bits to return.

Devuelve: An array of bits representing the value.

ToBits
public static bool[] ToBits(int value, int startBit, int bitCount)
result = CollectionHelper.ToBits(value, startBit, bitCount)

Converts an int value to a specified range of bits.

value
The int value to convert.
startBit
The starting bit position (0-based).
bitCount
The number of bits to return.

Devuelve: An array of bits representing the specified range.

ToBits
public static bool[] ToBits(float value)
result = CollectionHelper.ToBits(value)

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

value
The float value to convert.

Devuelve: An array of 32 bits representing the value.

ToBits
public static bool[] ToBits(long value)
result = CollectionHelper.ToBits(value)

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

value
The long value to convert.

Devuelve: An array of 64 bits representing the value.

ToComparer``1
public static IEqualityComparer<T> ToComparer<T>(Func<T, T, bool> comparer)
result = CollectionHelper.ToComparer(comparer)

Converts a function to an IEqualityComparer instance.

comparer
The function to determine equality between two objects.

Devuelve: An IEqualityComparer instance.

ToComparer``1
public static IComparer<T> ToComparer<T>(Func<T, T, int> comparer)
result = CollectionHelper.ToComparer(comparer)

Converts a function to an IComparer instance.

comparer
The function to compare two objects.

Devuelve: An IComparer instance.

ToComparer``1
public static IComparer<T> ToComparer<T>(Comparison<T> comparer)
result = CollectionHelper.ToComparer(comparer)

Converts a Comparison delegate to an IComparer instance.

comparer
The comparison delegate.

Devuelve: An IComparer instance.

ToDictionary``2
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey> comparer)
result = CollectionHelper.ToDictionary(source, comparer)

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

source
The sequence of key-value pairs.
comparer
The equality comparer for keys.

Devuelve: A dictionary containing the key-value pairs.

ToDictionary``2
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(IEnumerable<Tuple<TKey, TValue>> source)
result = CollectionHelper.ToDictionary(source)

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

source
The sequence of tuples.

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

ToDictionary``2
public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(IEnumerable<Tuple<TKey, TValue>> source, IEqualityComparer<TKey> comparer)
result = CollectionHelper.ToDictionary(source, comparer)

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

source
The sequence of tuples.
comparer
The equality comparer for keys.

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

ToDictionary``2
public static IDictionary<TKey, IEnumerable<TValue>> ToDictionary<TKey, TValue>(IEnumerable<IGrouping<TKey, TValue>> grouping)
result = CollectionHelper.ToDictionary(grouping)

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

grouping
The grouping to convert.

Devuelve: A dictionary mapping keys to their groups.

ToDictionary``3
public static IDictionary<TKey, TValue> ToDictionary<TSource, TKey, TValue>(IEnumerable<TSource> source, Func<TSource, int, TKey> keySelector, Func<TSource, int, TValue> valueSelector)
result = CollectionHelper.ToDictionary(source, keySelector, valueSelector)

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

source
The source sequence.
keySelector
The function to extract keys from elements.
valueSelector
The function to extract values from elements.

Devuelve: A dictionary containing the transformed key-value pairs.

ToFunc``1
public static Func<T, T, int> ToFunc<T>(Comparison<T> comparer)
result = CollectionHelper.ToFunc(comparer)

Converts a Comparison delegate to a function.

comparer
The comparison delegate.

Devuelve: A function that compares two objects.

ToIgnoreCaseSet
public static ISet<string> ToIgnoreCaseSet(IEnumerable<string> values)
result = CollectionHelper.ToIgnoreCaseSet(values)

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

values
The sequence of strings to convert.

Devuelve: A set containing the unique strings, ignoring case.

ToPairSet``2
public static PairSet<TKey, TValue> ToPairSet<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> source)
result = CollectionHelper.ToPairSet(source)

Converts a sequence of key-value pairs into a PairSet with the default equality comparer.

source
The sequence of key-value pairs.

Devuelve: A PairSet containing the key-value pairs.

ToPairSet``2
public static PairSet<TKey, TValue> ToPairSet<TKey, TValue>(IEnumerable<KeyValuePair<TKey, TValue>> source, IEqualityComparer<TKey> comparer)
result = CollectionHelper.ToPairSet(source, comparer)

Converts a sequence of key-value pairs into a PairSet with a specified equality comparer.

source
The sequence of key-value pairs.
comparer
The equality comparer for keys.

Devuelve: A PairSet containing the key-value pairs.

ToPairSet``3
public static PairSet<TKey, TValue> ToPairSet<TSource, TKey, TValue>(IEnumerable<TSource> source, Func<TSource, int, TKey> keySelector, Func<TSource, int, TValue> valueSelector)
result = CollectionHelper.ToPairSet(source, keySelector, valueSelector)

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

source
The source sequence.
keySelector
The function to extract keys from elements.
valueSelector
The function to extract values from elements.

Devuelve: A PairSet containing the transformed key-value pairs.

ToPair``2
public static KeyValuePair<TKey, TValue> ToPair<TKey, TValue>(ValueTuple<TKey, TValue> _)
result = CollectionHelper.ToPair(_)

Converts a value tuple to a key-value pair.

_
The value tuple to convert.

Devuelve: A KeyValuePair containing the tuple's key and value.

ToPair``2
public static KeyValuePair<TKey, TValue> ToPair<TKey, TValue>(Tuple<TKey, TValue> pair)
result = CollectionHelper.ToPair(pair)

Converts a tuple to a key-value pair.

pair
The tuple to convert.

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

ToTuple``2
public static Tuple<TKey, TValue> ToTuple<TKey, TValue>(KeyValuePair<TKey, TValue> pair)
result = CollectionHelper.ToTuple(pair)

Converts a key-value pair to a tuple.

pair
The key-value pair to convert.

Devuelve: A tuple containing the key and value.

TryAdd2``2
public static bool TryAdd2<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key, TValue value)
result = CollectionHelper.TryAdd2(dict, key, value)

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

dict
The dictionary to add the pair to.
key
The key to add.
value
The value to add.

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

TryAdd``1
public static void TryAdd<T>(ICollection<T> collection, IEnumerable<T> values)
CollectionHelper.TryAdd(collection, values)

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

collection
The collection to add values to.
values
The values to add.
TryAdd``1
public static bool TryAdd<T>(ICollection<T> collection, T value)
result = CollectionHelper.TryAdd(collection, value)

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

collection
The collection to add the value to.
value
The value to add.

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

TryDequeue2``1
public static T? TryDequeue2<T>(Queue<T> queue)
result = CollectionHelper.TryDequeue2(queue)

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

queue
The queue to dequeue from.

Devuelve: The dequeued item if available; otherwise, null.

TryDequeue2``1
public static T? TryDequeue2<T>(SynchronizedQueue<T> queue)
result = CollectionHelper.TryDequeue2(queue)

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

queue
The synchronized queue to dequeue from.

Devuelve: The dequeued item if available; otherwise, null.

TryDequeue``1
public static T TryDequeue<T>(Queue<T> queue)
result = CollectionHelper.TryDequeue(queue)

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

queue
The queue to dequeue from.

Devuelve: The dequeued item if available; otherwise, null.

TryDequeue``1
public static T TryDequeue<T>(SynchronizedQueue<T> queue)
result = CollectionHelper.TryDequeue(queue)

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

queue
The synchronized queue to dequeue from.

Devuelve: The dequeued item if available; otherwise, null.

TryGetAndRemove2``2
public static TValue? TryGetAndRemove2<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key)
result = CollectionHelper.TryGetAndRemove2(dict, key)

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

dict
The dictionary to operate on.
key
The key of the value to retrieve and remove.

Devuelve: The value if found and removed; otherwise, null.

TryGetAndRemove``2
public static TValue TryGetAndRemove<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key)
result = CollectionHelper.TryGetAndRemove(dict, key)

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

dict
The dictionary to operate on.
key
The key of the value to retrieve and remove.

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

TryGetAndRemove``2
public static bool TryGetAndRemove<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key, TValue value)
result = CollectionHelper.TryGetAndRemove(dict, key, value)

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

dict
The dictionary to operate on.
key
The key of the value to retrieve and remove.
value
When this method returns, contains the value if found; otherwise, the default value.

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

TryGetKey2``2
public static TKey? TryGetKey2<TKey, TValue>(PairSet<TKey, TValue> pairSet, TValue value)
result = CollectionHelper.TryGetKey2(pairSet, value)

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

pairSet
The pair set to query.
value
The value to look up.

Devuelve: The key if found; otherwise, null.

TryGetKey``2
public static TKey TryGetKey<TKey, TValue>(PairSet<TKey, TValue> pairSet, TValue value)
result = CollectionHelper.TryGetKey(pairSet, value)

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

pairSet
The pair set to query.
value
The value to look up.

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

TryGetValue2``2
public static TValue? TryGetValue2<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key)
result = CollectionHelper.TryGetValue2(dict, key)

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

dict
The dictionary to query.
key
The key to look up.

Devuelve: The value if found; otherwise, null.

TryGetValue``2
public static TValue TryGetValue<TKey, TValue>(IDictionary<TKey, TValue> dict, TKey key)
result = CollectionHelper.TryGetValue(dict, key)

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

dict
The dictionary to query.
key
The key to look up.

Devuelve: The value if found; otherwise, the default value of .

TryPeek2``1
public static T? TryPeek2<T>(SynchronizedQueue<T> queue)
result = CollectionHelper.TryPeek2(queue)

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

queue
The synchronized queue to peek into.

Devuelve: The next item if available; otherwise, null.

TryPeek2``1
public static T? TryPeek2<T>(Queue<T> queue)
result = CollectionHelper.TryPeek2(queue)

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

queue
The queue to peek into.

Devuelve: The next item if available; otherwise, null.

TryPeek``1
public static T TryPeek<T>(Queue<T> queue)
result = CollectionHelper.TryPeek(queue)

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

queue
The queue to peek into.

Devuelve: The next item if available; otherwise, null.

TryPeek``1
public static T TryPeek<T>(SynchronizedQueue<T> queue)
result = CollectionHelper.TryPeek(queue)

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

queue
The synchronized queue to peek into.

Devuelve: The next item if available; otherwise, null.

TypedAs``2
public static IDictionary<TKey, TValue> TypedAs<TKey, TValue>(IDictionary dictionary)
result = CollectionHelper.TypedAs(dictionary)

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

dictionary
The non-generic dictionary.

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

WhereNotNull``1
public static IEnumerable<T> WhereNotNull<T>(IEnumerable<T> enu)
result = CollectionHelper.WhereNotNull(enu)

Filters a sequence of nullable elements to exclude null values.

enu
Source sequence.

Devuelve: The sequence of non-null elements.

WhereNotNull``1
public static IEnumerable<T> WhereNotNull<T>(IEnumerable<T?> enu)
result = CollectionHelper.WhereNotNull(enu)

Filters a sequence of nullable elements to exclude null values.

enu
Source sequence.

Devuelve: The sequence of non-null elements.

WhereWithPrevious``1
public static IEnumerable<TSource> WhereWithPrevious<TSource>(IEnumerable<TSource> source, Func<TSource, TSource, bool> predicate)
result = CollectionHelper.WhereWithPrevious(source, predicate)

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

source
The sequence to filter.
predicate
The function to test each element against its previous element.

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