Arrays are data structures that store data in a continuous block of memory. Arrays are a built-in data structure in C# with multiple functions that are already implemented for the ease of development.
An array can be 1-D, 2-D, 3-D, and so on. The Array.Rank
property is one of the built-in functions for an array in C#. It is used to return the number of dimensions of an array.
The following syntax represents how this property is called:
Array.Rank
The Array
will be replaced by the custom name we give our array in the code.
Let's try to see how this property is accessed to give the array's dimensions:
using System;class HelloWorld{static void Main(){int[] row = {1,2,3,4};int[,] matrix = new int[3,2] {{2,1},{1,0},{3,1}};int[, ,] cube = new int[2, 2, 2]{{ { 1, 2}, { 3, 4} },{{3,2},{9,8}}};Console.WriteLine(row.Rank);Console.WriteLine(matrix.Rank);Console.WriteLine(cube.Rank);}}
Line 6: The row
array of the int type is 1-D.
Line 7: The matrix
array of the int type is 2-D.
Line 8: The cube
array of the int type is 3-D.
Line 12–14: When we use the .Rank
property, the output reflects the same when applied on a 1-D array. Similarly, the .Rank
property returns the number of dimensions for the other arrays.
Free Resources