56

How do you use the Array.GetLength function in C#?

What is the difference between the Length property and the GetLength function?

5 Answers 5

96

GetLength takes an integer that specifies the dimension of the array that you're querying and returns its length. Length property returns the total number of items in an array:

int[,,] a = new int[10,11,12];
Console.WriteLine(a.Length);           // 1320
Console.WriteLine(a.GetLength(0));     // 10
Console.WriteLine(a.GetLength(1));     // 11
Console.WriteLine(a.GetLength(2));     // 12
Sign up to request clarification or add additional context in comments.

5 Comments

And on one-dimensional arrays Length will return the same value as GetLength(0).
Why is a.GetLength(2) 11 and not 12?
@Mike: Cause 1 and 2 are a single key away.
Why is a.Length 1320? Why are the a.GetLength values, simply their values? This doesn't make any sense.
@Neo42 1320=10x11x12
16

For 1-dimensional arrays Length and GetLength(0) are exactly the same.

For arrays of higher rank Length is the product of all GetLength(0..Rank-1) values, in other words it is always the total number of fields.

3 Comments

int[] testArray = {1, 2, 3 , 4} is testArray.GetLength(1) correct?
No. Should be testArray.GetLength(0).
divo, "jagged arrays" are arrays of arrays, at each level rank==1. They are not multi-dimensional in the same way.
10

The .Length property returns the number of elements in an array, whether it be one dimensional or multidimensional. That is a 2x6 array will have length of 12.

The .GetLength(0) method returns number of elements in the row direction in a multidimensional array. For a 2x6 array that is 2.

The .GetLength(1) method returns number of elements in the column direction in a multidimensional array. For a 2x6 array that is 6.

These do not return an actual element value, as stated by the chosen answer above.

Comments

4

GetLength returns the length of a specified dimension of a mulit-dimensional array.

Length returns the sum of the total number of elements in all the dimensions.

  • For a single-dimensional array, Length == GetLength(0)
  • For a two-dimensional array, Length == GetLength(0) * GetLength(1)

etc.

2 Comments

Not quite true: Two dimensional arrays: Length = GetLength(0) * GetLength(1)
That's a clever typo! How did I manage to transpose + and * ?! (fixed)
0

In mathematical term, we call as m rows and n columns, so the results is product of m*n for a two dimensional array. In this case GetLength(0) = m rows and GetLength(1)= n columns. For e.g see below example

string[,] stocks ={{"RELIND","Reliance Industries","1006.30"},{"TATMOB","Tata Mobiles","504.10"},{"ALST","Allstate","800.00"}, {"GE","GE Motors","810.00"}
};

The stocks arrays return GetLength(0)= 4 and GetLength(1)=3 and length =12

1 Comment

GetLength(0)= 3 and GetLength(1)=2. It returns the index(which starts from zero) of the element, not the actual element count. I was confused by this for a minute. so I had to clarify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.