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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…