using System.Collections.Generic; using System.Text; namespace Ubisoft { public static class HtSystemCollections { public static string CollectionToString(this IEnumerable collection, string separator = ", ") { if (collection != null) { StringBuilder sb = new StringBuilder(); int i = 0; foreach (T item in collection) { if (item != null) { if (i > 0) { _ = sb.Append(separator); } _ = sb.Append(item.ToString()); i++; } } return sb.ToString(); } else { return ""; } } public static bool AddRange(this List source, List items, bool avoidDuplicates = false) { bool allAdded = true; if (items != null) { int count = items.Count; for (int i = 0; i < count; ++i) { if (!avoidDuplicates || !source.Contains(items[i])) { source.Add(items[i]); } else { allAdded = false; } } } return allAdded; } public static bool AddRange(this SortedSet source, IEnumerable items) { bool allAdded = true; foreach (T item in items) { allAdded &= source.Add(item); } return allAdded; } public static bool AddRange(this HashSet source, IEnumerable items) { bool allAdded = true; foreach (T item in items) { allAdded &= source.Add(item); } return allAdded; } public static int IndexOf(this IReadOnlyList self, T elementToFind) { int i = 0; foreach (T element in self) { if (Equals(element, elementToFind)) { return i; } i++; } return -1; } } }