Home » C# programs » C# basic programs

C# | different types of two dimensional array declarations

C# Example of two dimensional array with different types of array declarations: Here, we are going to learn how to declare a two dimensional array using different styles in C#?
Submitted by Pankaj Singh, on December 25, 2018

In the below example, we are declaring an integer array (two dimensional) with following styles:

1) Two dimensional Array declarations with initialization (without array size)

Example:

    int[,] arr1 = {
                    {1,2,3,4 },
                    {11,22,33,44}
                    };

In this style, arra1 will automatic occupy the size of the array (row and columns) based on given elements.

2) Dynamic Two dimensional Array declarations with fixed sizes

Example:

    int[,] arr2 = new int[2, 5];

In this style, arr2 will occupy the size for 2 rows and 5 columns (Integers) dynamically.

3) Dynamic Two dimensional Array declarations with variable sizes

Example:

    int[,] arr3 = new int[rows,cols];

In this style, we will read the size of the rows and cols for arr3 first and then declare the array dynamically.

Example: In the below given example – we are using these 3 styles to declare a two dimensional array.


Program:

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();
        }
    }
}

Output

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




Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.