internal static int AddRangeIfNotNull <T>(this ICollection <T> ts, IEnumerable <T> collection, bool enforceUnique = false) { if (ts == null) { throw new ArgumentNullException(nameof(ts)); } if (collection == null) { return(0); } if (collection.Count() < 1) { return(0); } int counter = 0; foreach (T item in collection) { if (ts.AddIfNotNull(item, enforceUnique)) { counter++; } } return(counter); }
/// <summary> /// Add a range of items to the collection (but only if not null). /// Also has an optional unique check. /// </summary> /// <typeparam name="T">Any Type</typeparam> /// <param name="ts">The collection to add the item to.</param> /// <param name="items">A range of items to add (if not null)</param> /// <param name="enforceUnique">Should the collection's items be unique? (If <see langword="true"/> and the item already exists in the collection, it will not be added).</param> /// <returns>The number of items added to the collection.</returns> public static int AddRangeIfNotNull <T>(this ICollection <T> ts, IEnumerable <T> items, bool enforceUnique = false) { if (ts == null) { throw new ArgumentNullException(nameof(ts)); } if (items.IsNullOrEmpty()) { return(0); } int counter = 0; foreach (T item in items) { if (ts.AddIfNotNull(item, enforceUnique)) { counter++; } } return(counter); }