I was just reading the C# specification and the part on array creation expressions. In the specification it says:
array-creation-expression:
new non-array-type [ expression-list ] rank-specifiersopt array-initializeropt
new array-type array-initializer
new rank-specifier array-initializer
[snip]
The dimension length expressions of the expression-list are evaluated in order, from left to right. Following evaluation of each expression, an implicit conversion (ยง6.1) to one of the following types is performed: int, uint, long, ulong. The first type in this list for which an implicit conversion exists is chosen. If evaluation of an expression or the subsequent implicit conversion causes an exception, then no further expressions are evaluated and no further steps are executed.
Excited, I thought hmm I don't think I've seen that yet, let's try a long dimension length:
bool[] bb = new bool[2L + Int32.MaxValue];
bb[int.MaxValue + 1L] = true;
Visual Studio says while pointing to the first line:
Unhandled Exception: System.OverflowException: Arithmetic operation resulted in an overflow.
Note this is NOT an "OutOfMemoryException". If I change my array creation expression and make it a little smaller:
bool[] bb = new bool[Int32.MaxValue];
This time I get an "OutOfMemoryException". I know about the whole "no object can be larger than 2GB" restriction from the CLR. My question is why am I getting a very different exception (OverflowException vs OutOfMemoryException) when the length is no longer convertible to Int32?