How to print 2D array in C#

4 Answers

0 votes
using System;
  
public static class Program
{
    public static void Main()
    {
        int[,] arr2d = new int[,] {
            {0, 1, 2, 3},
            {4, 5, 6, 7},  
            {8, 9, 10, 11}  
        };
  
        int rows = arr2d.GetLength(0);
        int cols = arr2d.GetLength(1);
  
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++)
                Console.Write("{0} ", arr2d[i, j]);
            Console.WriteLine();
        }
    }
}
  
  
  
  
/*
run:
  
0 1 2 3 
4 5 6 7 
8 9 10 11 
  
*/

 



answered Mar 11, 2023 by avibootz
0 votes
using System;
  
public static class Program
{
    public static void Main()
    {
        int[,] arr2d = new int[,] {
            {0, 1, 2, 3},
            {4, 5, 6, 7},  
            {8, 9, 10, 11}  
        };

        foreach (int num in arr2d) {
            Console.Write("{0} ", num);
        }
    }
}
  
  
  
  
/*
run:
  
0 1 2 3 4 5 6 7 8 9 10 11 
  
*/

 



answered Mar 11, 2023 by avibootz
0 votes
using System;
using System.Linq;

public static class Program
{
    public static void Main()
    {
        int[,] arr2d = new int[,] {
            {0, 1, 2, 3},
            {4, 5, 6, 7},  
            {8, 9, 10, 11}  
        };

        Console.WriteLine(String.Join(" ", arr2d.Cast<int>()));
    }
}
  
  
  
  
/*
run:
  
0 1 2 3 4 5 6 7 8 9 10 11
  
*/

 



answered Mar 11, 2023 by avibootz
0 votes
using System;
using System.Linq;
using System.Collections.Generic;

public static class Program
{
    public static void Main()
    {
        int[,] arr2d = new int[,] {
            {0, 1, 2, 3},
            {4, 5, 6, 7},  
            {8, 9, 10, 11}  
        };

        List<int> lst = arr2d.Cast<int>().ToList();
        
        lst.ForEach(Console.WriteLine);
    }
}
  
  
  
  
/*
run:
  
0
1
2
3
4
5
6
7
8
9
10
11
  
*/

 



answered Mar 11, 2023 by avibootz

Related questions

2 answers 226 views
1 answer 131 views
2 answers 231 views
1 answer 218 views
4 answers 353 views
5 answers 394 views
...