using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using JetBrains.Annotations; namespace UnityEngine.Rendering { /// /// A set of extension methods for collections /// public static class RemoveRangeExtensions { /// /// Tries to remove a range of elements from the list in the given range. /// /// The list to remove the range /// The zero-based starting index of the range of elements to remove /// The number of elements to remove. /// The exception raised by the implementation /// The value type stored on the list /// True if succeed, false otherwise [CollectionAccess(CollectionAccessType.ModifyExistingContent)] [MustUseReturnValue] public static bool TryRemoveElementsInRange([DisallowNull] this IList list, int index, int count, [NotNullWhen(false)] out Exception error) { try { if (list is List genericList) { genericList.RemoveRange(index, count); } else { if (index < 0) throw new ArgumentOutOfRangeException(nameof(index)); if (count < 0) throw new ArgumentOutOfRangeException(nameof(count)); if (list.Count - index < count) throw new ArgumentException("index and count do not denote a valid range of elements in the list"); for (var i = count; i > 0; --i) list.RemoveAt(index); } } catch (Exception e) { error = e; return false; } error = null; return true; } } }