Size of a Three-dimensional array in C#


To get the size of a 3D array in C#, use the GetLength() method with parameter as the index of the dimensions.

GetLength(dimensionIndex)

To get the size.

arr.GetLength(0)
arr.GetLength(1)
arr.GetLength(2)

Example

 Live Demo

using System;
class Program {
   static void Main() {
      int[,,] arr = new int[3,4,5];
      // length of a dimension
      Console.WriteLine(arr.GetLength(0));
      Console.WriteLine(arr.GetLength(1));
      Console.WriteLine(arr.GetLength(2));
      // length
      Console.WriteLine(arr.Length);
   }
}

Output

3
4
5
60

Updated on: 23-Jun-2020

893 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements