Table of Contents

Class GenericCollectionExtensions

Namespace
Excalibur.Dispatch.Extensions
Assembly
Excalibur.Dispatch.dll

Provides extension methods for converting collections to different collection types with optimized performance.

public static class GenericCollectionExtensions
Inheritance
GenericCollectionExtensions
Inherited Members

Remarks

These extension methods provide efficient conversions between different collection types, avoiding unnecessary allocations when possible and providing thread-safe operations.

Methods

AsArrayAsync<T>(Task<IEnumerable<T>>)

Asynchronously converts a Task of IEnumerable to an array.

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task<T[]> AsArrayAsync<T>(this Task<IEnumerable<T>> sourceTask)

Parameters

sourceTask Task<IEnumerable<T>>

The task that will return the source enumerable.

Returns

Task<T[]>

A task that represents the asynchronous operation. The task result contains the converted array.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when sourceTask is null.

AsArray<T>(IEnumerable<T>)

Converts an IEnumerable to an array with optimized handling for collections with known count.

public static T[] AsArray<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source enumerable to convert.

Returns

T[]

An array containing all elements from the source.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when source is null.

AsCollectionAsync<T>(Task<IEnumerable<T>>)

Asynchronously converts a Task of IEnumerable to a Collection.

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task<Collection<T>> AsCollectionAsync<T>(this Task<IEnumerable<T>> sourceTask)

Parameters

sourceTask Task<IEnumerable<T>>

The task that will return the source enumerable.

Returns

Task<Collection<T>>

A task that represents the asynchronous operation. The task result contains the converted Collection.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when sourceTask is null.

AsCollection<T>(IEnumerable<T>)

Converts an IEnumerable to a Collection.

public static Collection<T> AsCollection<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source enumerable to convert.

Returns

Collection<T>

A Collection containing all elements from the source.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when source is null.

AsListAsync<T>(Task<IEnumerable<T>>)

Asynchronously converts a Task of IEnumerable to a List.

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task<List<T>> AsListAsync<T>(this Task<IEnumerable<T>> sourceTask)

Parameters

sourceTask Task<IEnumerable<T>>

The task that will return the source enumerable.

Returns

Task<List<T>>

A task that represents the asynchronous operation. The task result contains the converted List.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when sourceTask is null.

AsList<T>(IEnumerable<T>)

Converts an IEnumerable to a List with optimized handling for common collection types.

public static List<T> AsList<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source enumerable to convert.

Returns

List<T>

A List containing all elements from the source.

Type Parameters

T

The type of elements in the collection.

Remarks

This method optimizes for common cases:

  • If source is already a List, it creates a new List to avoid external modifications
  • If source is an array or ICollection with known count, it pre-allocates the List capacity
  • Otherwise, it uses LINQ's ToList implementation.

Exceptions

ArgumentNullException

Thrown when source is null.

AsReadOnlyCollectionAsync<T>(Task<IEnumerable<T>>)

Asynchronously converts a Task of IEnumerable to a ReadOnlyCollection.

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task<ReadOnlyCollection<T>> AsReadOnlyCollectionAsync<T>(this Task<IEnumerable<T>> sourceTask)

Parameters

sourceTask Task<IEnumerable<T>>

The task that will return the source enumerable.

Returns

Task<ReadOnlyCollection<T>>

A task that represents the asynchronous operation. The task result contains the converted ReadOnlyCollection.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when sourceTask is null.

AsReadOnlyCollection<T>(IEnumerable<T>)

Converts an IEnumerable to a ReadOnlyCollection with optimized handling.

public static ReadOnlyCollection<T> AsReadOnlyCollection<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source enumerable to convert.

Returns

ReadOnlyCollection<T>

A ReadOnlyCollection containing all elements from the source.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when source is null.

AsReadOnlyListAsync<T>(Task<IEnumerable<T>>)

Asynchronously converts a Task of IEnumerable to an IReadOnlyList.

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task<IReadOnlyList<T>> AsReadOnlyListAsync<T>(this Task<IEnumerable<T>> sourceTask)

Parameters

sourceTask Task<IEnumerable<T>>

The task that will return the source enumerable.

Returns

Task<IReadOnlyList<T>>

A task that represents the asynchronous operation. The task result contains the converted IReadOnlyList.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when sourceTask is null.

AsReadOnlyList<T>(IEnumerable<T>)

Converts an IEnumerable to an IReadOnlyList with optimized handling.

public static IReadOnlyList<T> AsReadOnlyList<T>(this IEnumerable<T> source)

Parameters

source IEnumerable<T>

The source enumerable to convert.

Returns

IReadOnlyList<T>

An IReadOnlyList containing all elements from the source.

Type Parameters

T

The type of elements in the collection.

Exceptions

ArgumentNullException

Thrown when source is null.

IsNullOrEmpty<T>(IEnumerable<T>?)

Safely checks if a collection is null or empty.

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNullOrEmpty<T>(this IEnumerable<T>? source)

Parameters

source IEnumerable<T>

The source collection to check.

Returns

bool

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

Type Parameters

T

The type of elements in the collection.