Not that I would ever need to do this, but I want to understand how it works/doesn’t work. I googled quite a bit for the maximum length of an array and can’t really find anything.
long[] hugeArray = new long[long.MaxValue];
//No exceptions
Console.WriteLine("Init");
//Overflow exception
Console.WriteLine(hugeArray.LongLength.ToString());
hugeArray = new long[int.MaxValue];
//OutOfMemoryException
Console.WriteLine( hugeArray.Length.ToString());
And I guess a follow up question would be, if there is a limit and I am initializing outside that limit, why no exception when creating only when using? And is this something the compiler should catch?
According to SpankyJ, .NET 2.0 at least had a limit of 2 GB per array. 8 bytes (sizeof long) * ~2^31 = 16 gigabytes (not to mention the actual memory required). As for the Overflow, I agree with marcc that is probably because arrays are expected to be int-indexed (see e.g. this method Though I’m a bit uncertain regarding this, as this overload takes an array of Int64 lengths. This may be an extension only available in later versions.
Overall, though, this is mostly a theoretical issue. Anyone actually relying on this is likely doing something wrong.