| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="CollectionExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Linq; |
| | | 10 | | |
| | | 11 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 12 | | namespace MyNet.Collections; |
| | | 13 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 14 | | |
| | | 15 | | public static class CollectionExtensions |
| | | 16 | | { |
| | | 17 | | extension<T>(ICollection<T> collection) |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Replaces the content of the collection with the specified items. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="items">The items to set in the collection.</param> |
| | | 23 | | public void Set(IEnumerable<T>? items) |
| | | 24 | | { |
| | 6 | 25 | | collection.Clear(); |
| | 6 | 26 | | collection.AddRange(items); |
| | 6 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Adds a range of items to the collection. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="items">The items to add to the collection.</param> |
| | | 33 | | public void AddRange(IEnumerable<T>? items) |
| | | 34 | | { |
| | 6 | 35 | | ArgumentNullException.ThrowIfNull(collection); |
| | | 36 | | |
| | 6 | 37 | | var materialized = items?.ToArray() ?? []; |
| | 30 | 38 | | foreach (var item in materialized) |
| | | 39 | | { |
| | 9 | 40 | | collection.Add(item); |
| | | 41 | | } |
| | 6 | 42 | | } |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | |