Skip to main content

How to Create a Multidimensional Array in C#


In this tutorial, you will learn how to create a multidimensional array in your program in C#. We all know that an array a collection of variables of the same type that are referred to by a common name. Meaning, it is a storage variable that has different values. This is a continuation of my other tutorial entitled How to Create One-Dimensional Array in C#

To declare a two-dimensional array in C#, have this syntax:

  1. data type[ , ] array name = new data type[rows, columns];

To declare a three-dimensional array in C#, have this syntax:

  1. data type[ , , ] array name = new data type[size, size, size];

Now, let's start this tutorial!

1. Let's start with creating a Console Application in C# for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Console Application and name your project as MultidimensionalArray.

2. Now, put this code in your program.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace MultidimensionalArray
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. //declare a two dimensional string array
  13. string[,] array2DString = new string[3, 2] { { "one", "two" }, { "three", "four" },
  14. { "five", "six" } };
  15. //declare a two dimensional integer array
  16. int[,] array2DInt = new int[, ] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { 10, 11, 12 } };
  17.  
  18. //declare a three dimensional integer array
  19. int[, ,] array3DInt = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
  20. { { 7, 8, 9 }, { 10, 11, 12 } } };
  21.  
  22.  
  23. // array in two dimensional named array2DString
  24. Console.WriteLine("***Two Dimensional Array named array2Dstring***");
  25. Console.WriteLine(array2DString[0, 0]); //display one
  26. Console.WriteLine(array2DString[0, 1]); //display two
  27. Console.WriteLine(array2DString[1, 0]);//display three
  28. Console.WriteLine(array2DString[1, 1]);//display four
  29. Console.WriteLine(array2DString[2, 0]);//display five
  30. Console.WriteLine(array2DString[2, 1]);//display six
  31. Console.WriteLine("\n");
  32.  
  33. Console.WriteLine("***Two Dimensional Array named array2DInt***");
  34. Console.WriteLine(array2DInt[0,0]); // display 1
  35. Console.WriteLine(array2DInt[0, 1]); // display 2
  36. Console.WriteLine(array2DInt[0, 2]); // display 3
  37. Console.WriteLine(array2DInt[1, 0]);// display 4
  38. Console.WriteLine(array2DInt[1, 1]);// display 5
  39. Console.WriteLine(array2DInt[1, 2]);// display 6
  40. Console.WriteLine("\n");
  41.  
  42. Console.WriteLine("***Three-Dimensional Array named array3DInt***");
  43. Console.WriteLine(array3DInt[0, 0, 0]);// display 1
  44. Console.WriteLine(array3DInt[0, 0, 1]); // display 2
  45. Console.WriteLine(array3DInt[0, 0, 2]);// display 3
  46. Console.WriteLine(array3DInt[0, 1, 0]);// display 4
  47. Console.WriteLine(array3DInt[0, 1, 1]);// display 5
  48. Console.WriteLine(array3DInt[0, 1, 2]);// display 6
  49. Console.WriteLine(array3DInt[1, 0, 0]);// display 7
  50. Console.WriteLine(array3DInt[1, 0, 1]);// display 8
  51. Console.WriteLine(array3DInt[1, 0, 2]);// display 9
  52.  
  53.  
  54. }
  55. }
  56. }

We declare the two-dimensional array of string with the named array2DString that has the size of 3 rows and 2 columns that has a specified value in it as { "one", "two" }, { "three", "four" }, { "five", "six" }. To display the first element we have the code Console.WriteLine(array2DString[0, 0]); to display one. We all know that an array starts with 0, and String one was in row 0, and column 0. To display two, we declare the dimension of 0,1 because String 2 is in column 1. In the three-dimensional array, we have now two commas (, ,) referring to the three dimensions of the array. We have declared a three dimensional integer array named array3DInt. We did not specify the number of rows and columns in it to make sure to have no error as we put new int[,,] . Then it has the value inside of the 3D array { { 1, 2, 3 }, { 4, 5, 6 } }, { { 7, 8, 9 }, { 10, 11, 12 } }. To display the 1, we write this code Console.WriteLine(array3DInt[0, 0, 0]); Why it was 0,0,0 to display the 1? because 0 is the first in the array list.


Output:

output

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Subscribe at my YouTube Channel at: Ser Bermz

Visit my website at: www.campcodes.com

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.