C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

C# Multidimensional Array

In this tutorial, we will discuss multidimensional arrays in C#. Multidimensional arrays are arrays with more than one dimension, allowing you to store and access data in a table-like structure with rows and columns. C# supports two types of multidimensional arrays: rectangular arrays and jagged arrays.

  • Rectangular Arrays

Rectangular arrays are multidimensional arrays where all the rows have the same number of columns. They are created using commas to separate dimensions in the array declaration.

Here's an example of creating a 3x3 rectangular array:

int[,] rectangularArray = new int[3, 3];

In this example, we create a 3x3 rectangular array of integers. The two-dimensional array has three rows and three columns.

To assign values to a rectangular array, you can use nested loops:

for (int i = 0; i < 3; i++)
{
    for (int j = 0; j < 3; j++)
    {
        rectangularArray[i, j] = i * j;
    }
}

In this example, we use two nested loops to iterate over the rows and columns of the rectangularArray and assign values based on the row and column indices.

  • Jagged Arrays

Jagged arrays are arrays of arrays, where each row can have a different number of columns. They are created using multiple pairs of square brackets in the array declaration.

Here's an example of creating a jagged array with three rows:

int[][] jaggedArray = new int[3][];

In this example, we create a jagged array of integers with three rows. To define the number of columns for each row, you need to create an array for each row separately:

jaggedArray[0] = new int[2];
jaggedArray[1] = new int[3];
jaggedArray[2] = new int[4];

In this example, we create arrays with different lengths for each row of the jaggedArray. The first row has two columns, the second row has three columns, and the third row has four columns.

To assign values to a jagged array, you can use nested loops:

for (int i = 0; i < jaggedArray.Length; i++)
{
    for (int j = 0; j < jaggedArray[i].Length; j++)
    {
        jaggedArray[i][j] = i * j;
    }
}

In this example, we use two nested loops to iterate over the rows and columns of the jaggedArray and assign values based on the row and column indices.

  • Accessing Multidimensional Array Elements

To access the elements of a multidimensional array, you can use the indices of the row and column in square brackets.

For rectangular arrays:

int element = rectangularArray[1, 2];

For jagged arrays:

int element = jaggedArray[1][2];

In both examples, we access the element at row 1 and column 2 of the respective arrays.

This tutorial introduced multidimensional arrays in C#, including rectangular arrays and jagged arrays. Multidimensional arrays allow you to store and access data in a table-like structure with rows and columns, making them useful for various applications.

  1. How to create multidimensional arrays in C#

    Multidimensional arrays are created using commas to specify the size of each dimension.

    int[,] matrix = new int[3, 4];
    
  2. C# jagged arrays vs. multidimensional arrays

    • Multidimensional Array:

      int[,] matrix = new int[3, 4];
      
    • Jagged Array:

      int[][] jaggedArray = new int[3][];
      jaggedArray[0] = new int[4];
      jaggedArray[1] = new int[2];
      jaggedArray[2] = new int[3];
      
  3. Accessing elements in a multidimensional array in C#

    Access elements using indices for each dimension.

    int value = matrix[1, 2];
    
  4. Initializing multidimensional arrays in C#

    Initialize array elements during declaration:

    int[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 } };
    
  5. Working with rectangular arrays in C#

    Rectangular arrays have fixed dimensions, making them simpler than jagged arrays.

    int[,] rectangularArray = new int[2, 3];
    
  6. Dynamic multidimensional arrays in C#

    Use a loop to dynamically create and initialize multidimensional arrays.

    int[,] dynamicMatrix = new int[3, 4];
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            dynamicMatrix[i, j] = i * 4 + j + 1;
        }
    }
    
  7. C# multidimensional array vs. list of lists

    • Multidimensional Array:

      int[,] matrix = new int[3, 4];
      
    • List of Lists:

      List<List<int>> listOfLists = new List<List<int>>();
      
  8. Passing multidimensional arrays as parameters in C#

    Pass multidimensional arrays as method parameters.

    void ProcessArray(int[,] array)
    {
        // Processing logic
    }
    
  9. C# foreach loop with multidimensional arrays

    Use nested loops or GetLength method to iterate through elements.

    for (int i = 0; i < matrix.GetLength(0); i++)
    {
        for (int j = 0; j < matrix.GetLength(1); j++)
        {
            int value = matrix[i, j];
            // Process each element
        }
    }
    
  10. Sorting multidimensional arrays in C#

    Use methods like Array.Sort for sorting.

    Array.Sort(matrix, (a, b) => a[0].CompareTo(b[0]));
    
  11. C# LINQ queries with multidimensional arrays

    Utilize LINQ for querying multidimensional arrays.

    var result = from row in matrix.Cast<int>()
                 where row % 2 == 0
                 select row;
    
  12. C# jagged array to multidimensional array conversion

    Convert a jagged array to a multidimensional array using loops.

    int[][] jaggedArray = { new int[] { 1, 2 }, new int[] { 3, 4, 5 } };
    
    int[,] convertedArray = new int[jaggedArray.Length, jaggedArray.Max(subArray => subArray.Length)];
    for (int i = 0; i < jaggedArray.Length; i++)
    {
        for (int j = 0; j < jaggedArray[i].Length; j++)
        {
            convertedArray[i, j] = jaggedArray[i][j];
        }
    }