This documentation is archived and is not being maintained.
Type.GetArrayRank Method
.NET Framework 1.1
Gets the number of dimensions in an Array.
[Visual Basic] Public Overridable Function GetArrayRank() As Integer [C#] public virtual int GetArrayRank(); [C++] public: virtual int GetArrayRank(); [JScript] public function GetArrayRank() : int;
Return Value
An Int32 containing the number of dimensions in the current Type.
Exceptions
| Exception Type | 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. |
Example
[Visual Basic, C#, C++] The following example displays the number of dimensions in an array.
[Visual Basic] Imports System Imports Microsoft.VisualBasic Class MyArrayRankSample Public Shared Sub Main() Try Dim myArray(,,) As Integer = {{{12, 2, 35}, {300, 78, 33}}, {{92, 42, 135}, {30, 7, 3}}} Dim myType As Type = 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 e As NotSupportedException Console.WriteLine("NotSupportedException raised.") Console.WriteLine(("Source: " + e.Source)) Console.WriteLine(("Message: " + e.Message)) Catch e As Exception Console.WriteLine("Exception raised.") Console.WriteLine(("Source: " + e.Source)) Console.WriteLine(("Message: " + e.Message)) End Try End Sub 'Main End Class 'MyArrayRankSample [C#] 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); } } } [C++] #using <mscorlib.dll> using namespace System; int main() { try { Int32 myArray[,,] = new Int32[3,4,5]; Type* myType = myArray->GetType(); Console::WriteLine(S"myArray has {0} dimensions.",__box( myType->GetArrayRank())); } catch (NotSupportedException* e) { Console::WriteLine(S"NotSupportedException raised."); Console::WriteLine(S"Source: {0}", e->Source); Console::WriteLine(S"Message: {0}", e->Message); } catch (Exception* e) { Console::WriteLine(S"Exception raised."); Console::WriteLine(S"Source: {0}", e->Source); Console::WriteLine(S"Message: {0}", e->Message); } }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard
See Also
Type Class | Type Members | System Namespace | Array | Rank
Show: