C# | Different Types of Two Dimensional Array Declarations

Using this article, you can implement this C# | Different Types of Two Dimensional Array Declarations here. We will start the discussion below to successfully implement this task.

C# | Different Types of Two Dimensional Array Declarations

C# | Different Types of Two Dimensional Array Declarations

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Arrays2
{
    class Program
    {
        static void Main(string[] args)
        {
            //Multi Dimension 

            Console.WriteLine("Type 1 : Declaration");
            int[,] arr1 = {
                            {1,2,3,4 },
                            {11,22,33,44}
                          };
            for(int i=0;i<2;i++)
            {
                for(int j=0;j<4;j++)
                {
                    Console.Write("{0}\t", arr1[i, j]);
                }
                Console.WriteLine();
            }


            Console.WriteLine("\n\nType 2 : Declaration");
            int[,] arr2 = new int[2, 5];
            Console.WriteLine("Enter {0} values:", 2 * 5);
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    arr2[i, j]=Convert.ToInt32(Console.ReadLine());
                }
            }
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    Console.Write("{0}\t", arr2[i, j]);
                }
                Console.WriteLine();
            }

            Console.WriteLine("\n\nType 3 : Declaration");
            Console.Write("Enter Rows :");
            int rows = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter Cols :");
            int cols = Convert.ToInt32(Console.ReadLine());
            int[,] arr3 = new int[rows,cols];
            Console.WriteLine("Enter {0} values:", rows * cols);
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    arr3[i, j] = Convert.ToInt32(Console.ReadLine());
                }
            }
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    Console.Write("{0}\t", arr3[i, j]);
                }
                Console.WriteLine();
            }


            Console.ReadKey();
        }
    }
}
Type 1 : Declaration
1       2       3       4
11      22      33      44


Type 2 : Declaration
Enter 10 values:
10
10
10
20
20
20
30
30
30
40
10      10      10      20      20
20      30      30      30      40


Type 3 : Declaration
Enter Rows :2
Enter Cols :3
Enter 6 values:
1
1
1
9
9
9
1       1       1
9       9       9

Final Words

I hope this article helps you to understand C# | Different Types of Two Dimensional Array Declarations. If you encounter any issues let me know via the comment section.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment