C#.NET — Multidimensional arrays: compute .Length for a 3×2×3 array. Given: int[ , , ] a = new int[3, 2, 3]; Console.WriteLine(a.Length); What value is printed?

Difficulty: Easy

Correct Answer: 18

Explanation:

Introduction / Context:Here we practice reading array dimensions and using the Length property for a rectangular multidimensional array in C#. Length returns the total number of elements across all dimensions, not the size of any single dimension.

Given Data / Assumptions:

  • Array declaration: int[ , , ] a = new int[3, 2, 3].
  • Dimensions: 3 in the first, 2 in the second, 3 in the third.
  • We call a.Length and print it.

Concept / Approach:For a rectangular array with dimensions d0, d1, d2, the total element count is d0 * d1 * d2. The Length property returns exactly this product. It is not the highest index (use GetUpperBound for that) nor the number of dimensions (use Rank for that).

Step-by-Step Solution:

Compute total elements: 3 * 2 * 3 = 18. Therefore, a.Length is 18. Console.WriteLine prints 18.

Verification / Alternative check:Compare with Rank: a.Rank equals 3 (dimensions), but Length is 18 (elements). GetUpperBound(0) would be 2 for the first dimension (indices 0..2).

Why Other Options Are Wrong:

  • 20 and 10 are arbitrary and do not match the product.
  • 4 and 5 confuse Rank/Length or single-dimension sizes.

Common Pitfalls:Mixing up Length with Rank, or assuming Length equals the largest dimension only.

Final Answer:18

More Questions from Arrays

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion