I wanted to try to allocate a 4 billion bytes array and this is my C# code:
long size = 4 * 1000;
size *= 1000;
size *= 1000;
byte[] array = new byte[size];
this code fails with System.OverflowException
on the line containing new
. Okay, turns out Length
returns int
, so the array length is also limited to what int
can store.
Then why is there no compile-time error and long
is allowed to be used as the number of array elements at allocation?