How to Create a Multidimensional Array in C#
Submitted by donbermoy on Monday, June 30, 2014 - 12:50.
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:
To declare a three-dimensional array in C#, have this syntax:
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.
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:
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 Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace MultidimensionalArray
- {
- class Program
- {
- static void Main(string[] args)
- {
- //declare a two dimensional string array
- { "five", "six" } };
- //declare a two dimensional integer array
- //declare a three dimensional integer array
- { { 7, 8, 9 }, { 10, 11, 12 } } };
- // array in two dimensional named array2DString
- Console.WriteLine("***Two Dimensional Array named array2Dstring***");
- Console.WriteLine(array2DString[0, 0]); //display one
- Console.WriteLine(array2DString[0, 1]); //display two
- Console.WriteLine(array2DString[1, 0]);//display three
- Console.WriteLine(array2DString[1, 1]);//display four
- Console.WriteLine(array2DString[2, 0]);//display five
- Console.WriteLine(array2DString[2, 1]);//display six
- Console.WriteLine("\n");
- Console.WriteLine("***Two Dimensional Array named array2DInt***");
- Console.WriteLine(array2DInt[0,0]); // display 1
- Console.WriteLine(array2DInt[0, 1]); // display 2
- Console.WriteLine(array2DInt[0, 2]); // display 3
- Console.WriteLine(array2DInt[1, 0]);// display 4
- Console.WriteLine(array2DInt[1, 1]);// display 5
- Console.WriteLine(array2DInt[1, 2]);// display 6
- Console.WriteLine("\n");
- Console.WriteLine("***Three-Dimensional Array named array3DInt***");
- Console.WriteLine(array3DInt[0, 0, 0]);// display 1
- Console.WriteLine(array3DInt[0, 0, 1]); // display 2
- Console.WriteLine(array3DInt[0, 0, 2]);// display 3
- Console.WriteLine(array3DInt[0, 1, 0]);// display 4
- Console.WriteLine(array3DInt[0, 1, 1]);// display 5
- Console.WriteLine(array3DInt[0, 1, 2]);// display 6
- Console.WriteLine(array3DInt[1, 0, 0]);// display 7
- Console.WriteLine(array3DInt[1, 0, 1]);// display 8
- Console.WriteLine(array3DInt[1, 0, 2]);// display 9
- }
- }
- }

Add new comment
- 100 views