C# - Find Dimensions of an Array

Here, we are going to learn how to find the dimensions of an array in C#?
Submitted by Nidhi, on August 22, 2020 [Last updated : March 19, 2023]

Dimensions of an Array in C#

An array dimension is a direction in which you can vary the specification of an array's elements. C# programming supports multidimensional arrays that can vary up to 32 dimensions.

Problem statement

Here we will find out the dimensions of the given array, it means if an array is a one-d array then dimension will be 1 and if an array is a two-d array then dimension will be 2. In C# the "Rank" property is used to return the dimension of the specified array.

C# program to find the dimensions of an array

The source code to find out the dimensions of the specified array is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//Program to find out the dimensions of an array in C#

using System;

class Demo
{
    static void Main(string[] args)
    {
        int[]   OneD    = new int[10] ;
        int[,]  TwoD    = new int[3, 3];
        int[,,] ThreeD  = new int[2,3, 3];

        Console.WriteLine("Dimensions of OneD array is   : " + OneD.Rank);
        Console.WriteLine("Demensions of TwoD array is   : " + TwoD.Rank);
        Console.WriteLine("Demensions of ThreeD array is : " + ThreeD.Rank);
    }
}

Output

Dimensions of OneD array is   : 1
Demensions of TwoD array is   : 2
Demensions of ThreeD array is : 3
Press any key to continue . . .

Explanation

In the above program, we created three integer arrays OneD, TwoD, and ThreeD, and then we find out the dimensions of arrays using "Rank" property.

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.