Bug 8308 - Wrong handling of multidimensional array initializer
Summary: Wrong handling of multidimensional array initializer
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2012-11-09 06:37 UTC by Alex Willmy
Modified: 2012-11-09 10:24 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Alex Willmy 2012-11-09 06:37:58 UTC
Mono Version: 2.10.8.1
OS: Ubuntu 12.04

The following instantiation of a multidimensional array should throw a compiler error because the number of elements in each nested initializer must be the same. But on my machine this code compiles without errors.  


public static void Main (string[] args)
{
    int[,] a = new int[,] { {0, 1}, {2, 3, 4}, {5, 6} };

    Console.WriteLine (a[1, 0]);  // 2
    Console.WriteLine (a[1, 1]);  // 3
    // Console.WriteLine (a[1, 2]);  // IndexOutOfRangeException

    Console.WriteLine (a[2, 0]);  // 4
    Console.WriteLine (a[2, 1]);  // 5
}

From the C# spec:

For a multi-dimensional array, the array initializer must have as many levels of nesting as there are dimensions in the array. The outermost nesting level corresponds to the leftmost dimension and the innermost nesting level corresponds to the rightmost dimension. The length of each dimension of the array is determined by the number of elements at the corresponding nesting level in the array initializer. For each nested array initializer, the number of elements must be the same as the other array initializers at the same level.

http://msdn.microsoft.com/en-us/library/aa664573(v=vs.71).aspx
Comment 1 Marek Safar 2012-11-09 10:24:45 UTC
Already fixed in Mono 3.0