No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoveRange.Extensions.cs 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.CodeAnalysis;
  4. using JetBrains.Annotations;
  5. namespace UnityEngine.Rendering
  6. {
  7. /// <summary>
  8. /// A set of extension methods for collections
  9. /// </summary>
  10. public static class RemoveRangeExtensions
  11. {
  12. /// <summary>
  13. /// Tries to remove a range of elements from the list in the given range.
  14. /// </summary>
  15. /// <param name="list">The list to remove the range</param>
  16. /// <param name="index">The zero-based starting index of the range of elements to remove</param>
  17. /// <param name="count">The number of elements to remove.</param>
  18. /// <param name="error">The exception raised by the implementation</param>
  19. /// <typeparam name="TValue">The value type stored on the list</typeparam>
  20. /// <returns>True if succeed, false otherwise</returns>
  21. [CollectionAccess(CollectionAccessType.ModifyExistingContent)]
  22. [MustUseReturnValue]
  23. public static bool TryRemoveElementsInRange<TValue>([DisallowNull] this IList<TValue> list, int index, int count, [NotNullWhen(false)] out Exception error)
  24. {
  25. try
  26. {
  27. if (list is List<TValue> genericList)
  28. {
  29. genericList.RemoveRange(index, count);
  30. }
  31. else
  32. {
  33. #if DEVELOPMENT_BUILD || UNITY_EDITOR
  34. if (index < 0) throw new ArgumentOutOfRangeException(nameof(index));
  35. if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
  36. if (list.Count - index < count) throw new ArgumentException("index and count do not denote a valid range of elements in the list");
  37. #endif
  38. for (var i = count; i > 0; --i)
  39. list.RemoveAt(index);
  40. }
  41. }
  42. catch (Exception e)
  43. {
  44. error = e;
  45. return false;
  46. }
  47. error = null;
  48. return true;
  49. }
  50. }
  51. }