user10632736
user10632736

Reputation:

c# arrays out of bounds

        Random rnd = new Random();

        int[] numbeo = new int[100];

        for (int index = 0; index <= numbeo.GetLength(0) - 1; index++)
        {
            numbeo[index] = rng.Next(100);
            Console.WriteLine(numbeo[index]);
        }

I have worked with a company called funtech and the are string to show me how to do arrays in c# i understand it very well but the only thing I do not understand is when I need to to numbeo.GetLength - 1 why do i need to minus one.

I am thinking it is to do with this lets say for example I have an array of random numbers:

59, 64, 53, 4, 89. 0, 1, 2, 3, 4.

I am thinking when your try and use -1 it moves all the values backwards so now it does this.

59, 64, 53, 4, 89. 1, 2, 3, 4, 5.

Am I correct with this

Upvotes: 0

Views: 912

Answers (4)

GoldenAge
GoldenAge

Reputation: 3068

Its because the first index of the array is zero. So if your array is empty, then we can say it contains 0 elements and its length is also 0. If the array has 1 element in 0 index its length is 1. If the array has 2 elements, its length is 2 and so on and so forth. However, there are some languages where the index of an array starts at 1 not 0 (e.g. Fortran, Matlab, Smalltalk) but here we are talking about c#.

Upvotes: 0

LeBoucher
LeBoucher

Reputation: 406

No, it has nothing to do with the generated values. Arrays in C# are zero-based, meaning, that the first index of an array is 0.

string[] elements = new string[3]; // String array with 3 elements, indexes: 0, 1, 2
elements[0] = "Firt element"; // Ok
elements[1] = "Second element"; // Ok
elements[2] = "Third element"; // Ok
elements[3] = "Out of bounds"; //Throws an error

The .GetLength(0) method says that give me the length of the first dimension (0 is the first in dimensions). In 1 dimensional arrays, this is the same as .Length.

Console.WriteLine(elements.GetLenght(0)); // Prints 3 to console
Console.WriteLine(elements.Length); // Prints 3 to console

So you could use

numbeo.Length

The -1 is because the for loop uses less or equal condition, in your case. The for loop goes from 0 to 100, because your numbeo array's size is 100. And because 100 is the first invalid index in this array, you have to subtract 1, to go only to 99. In short, this is overcomplicated, use < instead of <=, and use .Length instead of GetLength(0)

    for (int index = 0; index < numbeo.Length; index++)
    {
        numbeo[index] = rng.Next(100);
        Console.WriteLine(numbeo[index]);
    }

This forloop goes from 0 to 99 (both ends inclusive), which are the exact valid range of this array's indexes.

Upvotes: 3

AGH
AGH

Reputation: 353

An arrays length always return depend on its size but index value always start with zero.

Exmaple : If you create an array int[] numbeo = new int[100] then length function will return length of 100 and array does not have 100 index value as it start with zero.

Console.Write("length :"+numbeo.GetLength(0)); 

Upvotes: 0

Try this

    Random rnd = new Random();

    int[] numbeo = new int[100];

    for (int index = 0; index < numbeo.Length  ; index++)
    {
        numbeo[index] = rng.Next(100);
        Console.WriteLine(numbeo[index]);
    }

Upvotes: -1

Related Questions