C
C
CityzenUNDEAD2020-08-24 11:35:10
Arrays
CityzenUNDEAD, 2020-08-24 11:35:10

Why does an error occur when accessing an element of a 2-dimensional array?

Hello everyone!
I don't understand why, when accessing an array element, the compiler throws an error
System.IndexOutOfRangeException: "Index was outside the bounds of the array."

int[,] mass = new int[1,2];
mass[1,2] = 14; - здесь вылетает ошибка

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-08-24
@CityzenUNDEAD

Arrays are counted from zero, and you are trying to write something to the 3rd cell, although from 2

C
CityzenUNDEAD, 2020-08-24
@CityzenUNDEAD

It's clear. It seemed to me that when you initialize an array and prescribe how many elements it should have, there is also a countdown from zero. It turned out not.
Thanks to all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question