• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Multidimensional Arrays in C#

December 14, 2021 C#

In this post, you will learn how to work with multidimensional arrays so that you can use that same knowledge and thus evolve.

  • a multidimensional array (array) is basically a table with rows and columns or it can be much more than a table (depending on the number of values that make up the multidimensional array);
  • has a fixed size and is used to store a dataset in a sequential way;
  • it is important to remember that it is more useful to think of an array as a set of variables of the same type in which they are placed in memory adjacently;
  • these variables are called elements and each one of these is identified by an index, and we have an index to identify a particular column and another index to identify a particular row.
using System;

namespace Base {
  class MultidimensionalArrays {
    public void Run() {
      // [number of rows, number of columns]
      int[,] multidimensionalArray = new int[5, 4] {
        { 1, 2, 3, 4 },
        { 1, 1, 1, 1 },
        { 2, 2, 2, 2 },
        { 3, 3, 3, 3 },
        { 4, 4, 4, 4 }
      };

      for (int i = 0; i < multidimensionalArray.GetLength(0); i++) {
        for (int j = 0; j < multidimensionalArray.GetLength(1); j++) {
          Console.Write($"{multidimensionalArray[i, j]}\t");
        }

        Console.WriteLine();
      }
    }
  }
}

/*
  1  2  3  4
  1  1  1  1
  2  2  2  2
  3  3  3  3
  4  4  4  4
*/

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved