krahets/hello-algo · error · IndexOutOfRangeException

индекс выходит за границы

Error message

индекс выходит за границы

What it means

IndexOutOfRangeException with message "индекс выходит за границы" (Russian for "index out of bounds") is thrown by MyList.Get(int index) when index < 0 or index >= arrSize. The logic is identical to the English version; only the message string differs.

Source

Thrown at ru/codes/csharp/chapter_array_and_linkedlist/my_list.cs:35

    public MyList() {
        arr = new int[arrCapacity];
    }

    /* Получить длину списка (текущее число элементов) */
    public int Size() {
        return arrSize;
    }

    /* Получить вместимость списка */
    public int Capacity() {
        return arrCapacity;
    }

    /* Доступ к элементу */
    public int Get(int index) {
        // Если индекс выходит за границы, выбрасывается исключение; далее аналогично
        if (index < 0 || index >= arrSize)
            throw new IndexOutOfRangeException("индекс выходит за границы");
        return arr[index];
    }

    /* Обновление элемента */
    public void Set(int index, int num) {
        if (index < 0 || index >= arrSize)
            throw new IndexOutOfRangeException("индекс выходит за границы");
        arr[index] = num;
    }

    /* Добавление элемента в конец */
    public void Add(int num) {
        // При превышении вместимости по числу элементов запускается расширение
        if (arrSize == arrCapacity)
            ExtendCapacity();
        arr[arrSize] = num;
        // Обновить число элементов
        arrSize++;

View on GitHub (pinned to 69932aed18)

Solutions

  1. Validate 0 <= index < list.Size() before calling Get().
  2. Correct loop bounds to i < Size().
  3. Clamp or reject indices from external input.

Example fix

// before
MyList list = new();
list.Add(10);
int val = list.Get(1); // throws

// after
if (index >= 0 && index < list.Size())
    int val = list.Get(index);
Defensive patterns

Strategy: validation

Validate before calling

if (index >= 0 && index < list.Size()) {
    int val = list.Get(index);
}

Try / catch

try {
    int val = list.Get(index);
} catch (IndexOutOfRangeException ex) when (ex.Message.Contains("выходит за границы")) {
    // handle out-of-bounds
}

Prevention

When it happens

Trigger: Calling Get(index) where index < 0 or index >= list.Size(). An empty MyList (arrSize 0) throws for any index including 0.

Common situations: Off-by-one iteration bounds, reading from an unpopulated list, or using an externally-supplied index without validation. Russian-speaking developers may search for the localized message.

Related errors


AI-assisted analysis of krahets/hello-algo@69932aed18 (2026-08-13). Data as JSON: /api/errors/cb24d2cafab8dd88. Report an issue: GitHub.