How to use Array.GetLength() method to get number of elements in the specified dimension of an Array in C#

3 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Array arr1D = Array.CreateInstance(typeof(int), 7);

            Console.WriteLine(arr1D.GetLength(0));
        }
    }
}


/*
run:
 
7

*/

 



answered Apr 24, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Array arr2D = Array.CreateInstance(typeof(int), 6, 3);

            Console.WriteLine("dimension[0] = {0}", arr2D.GetLength(0));
            Console.WriteLine("dimension[1] = {0}", arr2D.GetLength(1));
        }
    }
}


/*
run:
  
dimension[0] = 6
dimension[1] = 3
 
*/

 



answered Apr 24, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Array arr3D = Array.CreateInstance(typeof(int), 6, 4, 3);

            Console.WriteLine("dimension[0] = {0}", arr3D.GetLength(0));
            Console.WriteLine("dimension[1] = {0}", arr3D.GetLength(1));
            Console.WriteLine("dimension[2] = {0}", arr3D.GetLength(2));
        }
    }
}


/*
run:
 
dimension[0] = 6
dimension[1] = 4
dimension[2] = 3

*/

 



answered Apr 24, 2016 by avibootz
...