Just wanted to know why this line throws an excpetion on runtime but compiles fine.
Also what is the best way to prevent this?
var vowelsArray = new char['a', 'e', 'i', 'o', 'u'] ; // Is of type char[,,,,]
and this one works since it's a normal char array
var vowels = new char[] { 'a', 'e', 'i', 'o', 'u' }; // Is of type char[]?
https://dotnetfiddle.net/pulJI4
In the output window you'll see the OutOfMemory Exception
For anyone wondering: Like @MongZhu said, it is a jagged Array. So, when we have
int[] array1 = new int[5];
00
00
00
00
00
it creates an array within an array at the first dimension with 5 entries.
But as soon as we add, another dimension i.e
int[,] array1 = new int[5,2];
0 1
0 00 00
1 00 00
2 00 00
3 00 00
4 00 00
The size of the array is suddenly doubled.
The array size is multiplied by the result of the previous indexies. Meaning that if we were to have an array with [5,2,3] we would get 5 * 2 * 3 = 30
To answer my own question, why the jagged array caused an exception:
The array size increased since the char gets converted into an number to get a size out of it. Here the letter 'a' represents the number 97, therefor all the vowels respond to numbers that multiply each other. Which are:
a 97
i 105
e 101
o 111
u 117
This leads to an array with entries: 13’359’532’095 (more than 13 billion characters). And since .Net uses the full Unicode 4-byte encoding for characters, it works out as an attempt to allocate nearly 50 GB of RAM.