SequenceUtility.cs 878 B

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace NModbus.Unme.Common
  5. {
  6. internal static class SequenceUtility
  7. {
  8. public static IEnumerable<T> Slice<T>(this IEnumerable<T> source, int startIndex, int size)
  9. {
  10. if (source == null)
  11. {
  12. throw new ArgumentNullException(nameof(source));
  13. }
  14. var enumerable = source as T[] ?? source.ToArray();
  15. int num = enumerable.Count();
  16. if (startIndex < 0 || num < startIndex)
  17. {
  18. throw new ArgumentOutOfRangeException(nameof(startIndex));
  19. }
  20. if (size < 0 || startIndex + size > num)
  21. {
  22. throw new ArgumentOutOfRangeException(nameof(size));
  23. }
  24. return enumerable.Skip(startIndex).Take(size);
  25. }
  26. }
  27. }