Get the number of dimensions in an Array in CSharp

Description

The following code shows how to get the number of dimensions in an Array.

Example


//from w ww . j a  va 2  s  .c  om
using System;

class MyArrayRankSample
{
    public static void Main()
    {
       int[,,] myArray = new int[,,] {{{12,2,35},{3,78,1}},{{92,42,1},{30,7,3}}};
       Type myType = myArray.GetType();

       Console.WriteLine("Contents of myArray: {{{12,2,35},{1,78,1}},{{92,42,1},{30,7,3}}}");
       Console.WriteLine("myArray has {0} dimensions.", myType.GetArrayRank());
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Reflection »




Array
Constructor
Event
Field
Interface
Method
Properties
Type