Uchenna

IndexOutOfRangeException: Index was outside the bounds of the array.

net Icon

System.IndexOutOfRangeException

Index was outside the bounds of the array.

An unhandled exception occurred on line 19
16
    public int GetNumberOfPeople()
17
    {
18
        var numberOfPeople = new []{3, 5, 7, 4, 2, 1, 21, 17, 13};
19
        return numberOfPeople[-1];
20
    }

Solution

Instead of using -1 to access last item, use ^1            
Sep 30, 2022 Anyone

0 replies


Install the browser extension

Found this solution helpful? Try our extension for quick solutions to your .NET errors.

Chrome logo Add to Chrome - It's free