How to find 2D array (matrix) dimensions (rows, cols) in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] matrix = new int[,] { { 1, 1}, { 2, 2}, { 3, 3}, { 4, 4} };

            int rows = matrix.GetUpperBound(0) + 1;
            int cols = matrix.GetUpperBound(1) + 1;

            Console.WriteLine("rows = {0} ", rows);
            Console.WriteLine("cols = {0} ", cols);

            // for (i = 0; i < rows; i++)...
            // for (i = 0; i <= rows - 1; i++)...
        }
    }
}

/*
run:
    
rows = 4
cols = 2
   
*/

 



answered May 24, 2017 by avibootz
0 votes
using System;
 
namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] matrix = new int[3, 5] { { 12, 43, 65, 10, 12 },
                                            { 33, 42, 98, 80, 50 },
                                            { 88, 77, 99, 100, 9843 } };
 
            Console.WriteLine("rows: {0}", matrix.GetLength(0));
            Console.WriteLine("cols: {0}", matrix.GetLength(1));
        }
    }
}
 
 
/*
run:
       
rows: 3
cols: 5
   
*/

 



answered Apr 14, 2018 by avibootz

Related questions

1 answer 274 views
2 answers 434 views
1 answer 313 views
1 answer 336 views
...