What is Array.Getlength for?

Asked

Viewed 870 times

4

To know the size of a vector, I can only use the nomedovetor.Length, right?

I saw an example in a structure for, where they wore GetLength(0) or GetLength(1) to capture the dimensions of a array row and column.

Could you explain this to me 0 and 1 in parentheses? I didn’t understand it very well.

Thanks in advance!

1 answer

7

To know the size of one vector, I can only use the nomedovetor.Length, right?

Depends, if it’s a array multidimensional the Array.GetLength() should be more appropriate, it takes as argument the dimension of array, and will return the amount of such elements array, the Array.Length will return all elements of all arrays.

Could you explain this to me 0 and 1 in brackets?

These values indicate the size of the array that you want to get the length, Array.GetLength(0) is the same as Array.Length, when applied in a array of one dimension.

See a comparison between the two:

string[,] matriz = new string[,]{
    {"1.1", "1.2", "1.3"},
    {"2.1", "2.2", "2.3"},
    {"3.1", "3.2", "3.3"}
};

Console.WriteLine(String.Format("Length: {0}", matriz.Length));          // 9
Console.WriteLine(String.Format("GetLength: {0}", matriz.GetLength(0))); // 3

See demonstração

Browser other questions tagged

You are not signed in. Login or sign up in order to post.