GetArrayRank Method
TOC
Collapse the table of content
Expand the table of content

Type.GetArrayRank Method ()

 

Gets the number of dimensions in an array.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public virtual int GetArrayRank()

Return Value

Type: System.Int32

An integer that contains the number of dimensions in the current type.

Exception Condition
NotSupportedException

The functionality of this method is unsupported in the base class and must be implemented in a derived class instead.

ArgumentException

The current type is not an array.

The following example displays the number of dimensions in an array.

using System;

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

            Console.WriteLine("Contents of myArray: {{{12,2,35},{300,78,33}},{{92,42,135},{30,7,3}}}");
            Console.WriteLine("myArray has {0} dimensions.", myType.GetArrayRank());
        }
        catch(NotSupportedException e)
        {
            Console.WriteLine("NotSupportedException raised.");
            Console.WriteLine("Source: " + e.Source);
            Console.WriteLine("Message: " + e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception raised.");
            Console.WriteLine("Source: " + e.Source);
            Console.WriteLine("Message: " + e.Message);
        }      
    }
}

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft