Search code examples
arraysf#c#-to-f#gcallowverylargeobjects

how do you create a large array?


Given

// r is a System.Data.IDataRecord
var blob = new byte[(r.GetBytes(0, 0, null, 0, int.MaxValue))];
r.GetBytes(0, 0, blob, 0, blob.Length);

and r.GetBytes(...) returns Int64 Since Array.zeroCreate and Array.init take Int32 how do I create an empty array that is potentially larger than Int32.MaxValue?


Solution

  • You can't. The maximum length of a single-dimensional array on .NET is System.Int32.MaxValue. Your C# code raises an OverflowException when the value is greater than this limit. The equivalent F# code is:

    let blob =         
        let length = r.GetBytes(0, 0, null, 0, Int32.MaxValue)
        if length > int64(Int32.MaxValue) then
            raise (OverflowException())
        else
            Array.zeroCreate<byte>(int32(length))