C# | different types of two dimensional array declarations

Learn, how to declare a two dimensional array using different styles in C#?
Submitted by Pankaj Singh, on December 25, 2018 [Last updated : March 19, 2023]

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.


C# program for 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();
    }
  }
}

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

C# Basic Programs »


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.