Consider the following:
long size = int.MaxValue;
long[] huge = new long[size]; // throws OutOfMemoryException
long[] huge = new long[size + 1]; // throws OverflowException
I know there is a 2GB limit on the size of a single object, which explains the first exception, but why do I get a different exception once the number of elements surpasses 32bits?
(I am using a 64-bit computer if that's important).
EDIT: I can also define and use an indexer that accepts a long
with no problems:
internal sealed class MyClass
{
public object this[long x]
{
get
{
Console.WriteLine("{0}", x);
return null;
}
}
}
...
long size = int.MaxValue;
MyClass asdf = new MyClass();
object o = asdf[size * 50]; // outputs 107374182350
C# arrays are indexed by System.Int32
. Since size + 1
is beyond Int32.MaxValue
, you get an integer overflow.
Use the overload of Array.CreateInstance
that takes a long instead, if you really want to use a long
as index.
So from what I've gathered, something like the following is happening here:
<= Int32.MaxValue
and will throw an overflow exception for a value that exceeds Int32.MaxValue
.While the latter point feels like some kind of weird contradiction (accepting types larger than Int32
, but throwing an exception if you happen to actually use any of those extra bits), it is apparently a side-effect of the fact that some of this is half-implemented for the future implementation of arrays which will be allowed to have more than Int32.MaxValue
elements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With