Export (0) Print
Expand All
Expand Minimize

Type.GetArrayRank Method

Gets the number of dimensions in an Array.

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

public virtual int GetArrayRank ()
public int GetArrayRank ()
public function GetArrayRank () : int
Not applicable.

Return Value

An Int32 containing the number of dimensions in the current Type.

Exception typeCondition

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);
        }      
    }
}

import System.*;

class MyArrayRankSample
{
    public static void main(String[] args)
    {
        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.",
                System.Convert.ToString(myType.GetArrayRank()));
        }
        catch (NotSupportedException e) {
            Console.WriteLine("NotSupportedException raised.");
            Console.WriteLine("Source: " + e.get_Source());
            Console.WriteLine("Message: " + e.get_Message());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception raised.");
            Console.WriteLine("Source: " + e.get_Source());
            Console.WriteLine("Message: " + e.get_Message());
        }
    } //main
} //MyArrayRankSample

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft