namespace System.Collections.Generic
{
///
/// Provides extension methods for generic implementations.
///
public static class CollectionExtensions
{
///
/// Add the to the if it is not .
///
/// The type of the elements in the collection
/// The collection.
/// The item to add if not .
public static void AddIfNotNull(this ICollection collection, T item)
{
#if NET8_0_OR_GREATER
ArgumentNullException.ThrowIfNull(collection);
#else
if (collection == null)
throw new ArgumentNullException(nameof(collection));
#endif
if (item == null)
return;
collection.Add(item);
}
///
/// Adds a functionallity to .
///
/// The type of the elements in the collection.
/// The collection.
/// The items to add.
public static void AddRange(this ICollection collection, IEnumerable items)
{
#if NET8_0_OR_GREATER
ArgumentNullException.ThrowIfNull(collection);
ArgumentNullException.ThrowIfNull(items);
#else
if (collection == null)
throw new ArgumentNullException(nameof(collection));
if (items == null)
throw new ArgumentNullException(nameof(items));
#endif
if (collection == items)
return;
foreach (var item in items)
collection.Add(item);
}
}
}