Build array index ranges of an integer in C# .NET
February 16, 2017 Leave a comment
Suppose that we have a large array of data heavy objects that are impractical to handle in a single go. Instead we can read batches of the array until all elements have been processed. It can then be useful to build a range of indexes for the objects in the array that we want to extract.
E.g. if an array consists of 357 objects and we want to read at most 100 elements from it in a single batch then the size of the batches will be as follows:
100
100
100
57
…and we want to extract the elements by their indexes as follows:
0, 99
100, 199
200, 299
300, 356
…where the first number is the array start index and the second number is the array end index. Since we’re talking about array indexes the numbers are 0-based of course.