There is an array whose size can potentially be long . Well, I need to make clippings from this array using Skip() and Take() , but they take int as a parameter. You can, of course, use a brute-grip with handles, you can even do your own extension methods for this, but maybe there are regular ways?

  • one
    The presence of such an array usually means that you have something badly wrong with the architecture. - Pavel Mayorov

2 answers 2

No, there are no such extensions and cannot be for one simple reason.

Linq does not work with arrays . Linq2Objects works with sequences in which the next element can be obtained only after the previous one. Therefore, an operation like Skip (4000000000) will be performed for a long time - it will have to go through all the elements that are skipped! But the worst thing is that it will be performed meaninglessly long - because the same effect can be obtained much faster if you abandon the abstraction of the sequence.

PS at the moment from all runtimes only Mono supports the creation of arrays larger than 2Gb in size. Are you sure you want to write such an intolerable code?

  • Thank you for your comment. - iRumba
  • And what will happen if I execute this? Var bytes = File.ReadAllBytes (fileName); for a very large file? After all, in NTFS the maximum file size is 2 ^ 64 bytes - iRumba
  • @iRumba will be OutOfMemoryException - Pavel Mayorov
  • @iRumba is the reason why large files cannot be read via ReadAllBytes - Pavel Mayorov
  • one
    @iRumba if you think about this problem - it's time to carefully study a class like FileStream - Pavel Mayorov

If we talk specifically about arrays, then the standard CLR uses the Int32 type for its index. Therefore, theoretically, an array can contain no more than Int32.MaxValue elements (2,147,483,647). If the array is multidimensional, this restriction applies to each of its dimensions. But that is not all. Before .NET 4.5, the maximum size of a single object (including an array) on the heap was limited to 2 GB, even for a 64-bit platform. Therefore, an attempt to create an array, for example, byte[] with Int32.MaxValue elements will exhaust the memory limit for a single object.

In .NET 4.5, the gcAllowVeryLargeObjects parameter was added, which allows creating arrays with the maximum number of elements equal to UInt32.MaxValue (4,294,967,295). However, for each dimension, the maximum allowable index is still limited:

This is a unique index of the individual index of the individual index is 2,147,483,591 (0x7FFFFFC7) for the arrays and 2,146,435,071 (0X7FEFFFFF) for the other types.

Therefore, Skip / Take with the long type does not make sense for arrays.

Of course, you can use, for example, a linked list, where the number of elements is limited only by the available memory. But, as already answered, this indicates the presence of some problems in the application architecture.