Table of Contents

Class CollectionExtensions

Namespace
Cuemon.Extensions.Collections.Generic
Assembly
Cuemon.Extensions.Collections.Generic.dll

Extension methods for the ICollection<T> interface.

public static class CollectionExtensions
Inheritance
CollectionExtensions

Examples

CollectionExtensions provides extension methods for ICollection<T> including AddRange for bulk insertion and ToPartitioner for batched iteration. This example creates an empty List<int>, calls AddRange(1, 2, 3, 4) to insert four integers at once, then creates a partitioner with ToPartitioner(2) to split elements into batches of two. Console output confirms the element count (4) and the partition count (2), demonstrating batch processing of collection contents.

using System;
using System.Collections.Generic;
using System.Linq;
using Cuemon.Extensions.Collections.Generic;

namespace MyApp.Examples;

public static class CollectionExtensionsExample
{
    public static void Demonstrate()
    {
        ICollection<int> values = new List<int>();
        values.AddRange(1, 2, 3, 4);

        var partitioner = values.ToPartitioner(2);

        Console.WriteLine(values.Count);
        Console.WriteLine(partitioner.Count());
    }
}

Methods

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

Adds the elements of the specified source to the collection.

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

Parameters

collection ICollection<T>

The ICollection<T> to extend.

source IEnumerable<T>

The sequence of elements that should be added to collection.

Type Parameters

T

The type of elements in the ICollection<T>.

AddRange<T>(ICollection<T>, params T[])

Adds the elements of the specified source to the collection.

public static void AddRange<T>(this ICollection<T> collection, params T[] source)

Parameters

collection ICollection<T>

The ICollection<T> to extend.

source T[]

The sequence of elements that should be added to collection.

Type Parameters

T

The type of elements in the ICollection<T>.

ToPartitioner<T>(ICollection<T>, int)

Extends the specified collection to support iterating in partitions.

public static PartitionerCollection<T> ToPartitioner<T>(this ICollection<T> collection, int partitionSize = 128)

Parameters

collection ICollection<T>

The ICollection<T> to extend.

partitionSize int

The size of the partitions.

Returns

PartitionerCollection<T>

An instance of PartitionerCollection<T>.

Type Parameters

T

The type of elements in the collection.