0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Changed from *int[,] matrix = new int[2, 2];*
            int[,] matrix = new int[3, 3];
            // Still getting error with "[3,3]"

            matrix[0, 0] = 1;
            matrix[0, 1] = 2;
            matrix[0, 2] = 3;

            matrix[1, 0] = 4;
            matrix[1, 1] = 5;
            matrix[1, 2] = 6;

            matrix[2, 0] = 7;
            matrix[2, 1] = 8;
            matrix[2, 2] = 9;

            Console.Write(matrix[0, 2]);

            Console.ReadKey();
        }
    }
}

Here is a basic program to execute through command line.

Upon running, instead of displaying the number "3" stored in array [0,2], I am presented with this error:

System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'

Jojo
  • 11
  • 4
  • it should be `new int[3, 3];`, the numbers here mean the lengths of each dimension. – King King Nov 04 '17 at 04:28
  • You might want to look at this: https://stackoverflow.com/questions/3814145/how-can-i-declare-a-two-dimensional-string-array – Evan Trimboli Nov 04 '17 at 04:31
  • The book I am reading gave me the impression that arrays count upwards from zero, and therefore 0, 1, 2 would count as "three". – Jojo Nov 04 '17 at 04:50
  • Arrays in C# do start at `0`. But the number you pass when you create an array is the length, not the maximum index. So `new int[2, 2]` creates a 2x2 array, with maximum indexes for each dimension of `1`. If you want a 3x3 array, you need `new int[3,3]` – Peter Duniho Nov 04 '17 at 05:02
  • Hmm, a change to [3,3] yields the same error. – Jojo Nov 04 '17 at 05:24
  • but there no error in your code now. – A.D. Nov 04 '17 at 05:47

3 Answers3

0

"new int[2, 2];" means the matrix is 2 x 2. You are accessing the 3rd column by matrix[0, 2], hence the exception.

Mike Mat
  • 632
  • 7
  • 15
0

@josias int[,] matrix = new int[2, 2]; shows that you have a matrix with 2 rows and 2 columns. But in your code, you assign the values in 3 rows and 3 column. Please use the following code if you have such values.

int[,] matrix = new int[3, 3];

A.D.
  • 2,352
  • 2
  • 15
  • 25
0

This is directly from C# Specifications:

Each dimension of an array has an associated length which is an integral number greater than or equal to zero. The dimension lengths are not part of the type of the array, but rather are established when an instance of the array type is created at run-time.

And now this is the part which answers your question:

The length of a dimension determines the valid range of indices for that dimension: For a dimension of length N, indices can range from 0 to N - 1 inclusive.

Therefore, in your case the range will be 0 to 2 - 1 which 0 and 1. In some languages such as VB.NET, your assumption will be correct but not in C#.

Also, please read this SO thread.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64