Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Multi-Dimensional Arrays | Arrays
C# Basics

book
Multi-Dimensional Arrays

Arrays can also have additional arrays inside them. Such arrays are called multi-dimensional arrays. These are useful when we want to store the data in a tabular form, with rows and columns, or in the form of a matrix.

We can declare a two-dimensional array using the following syntax:

main.cs

main.cs

copy
datatype[][] arrayName = new datatype[lengthX, lengthY];
1
datatype[][] arrayName = new datatype[lengthX, lengthY];

The array created using the above syntax will have the length (size) equal to lengthX and each element will be an array of size lengthY:

main.cs

main.cs

copy
int[,] numbers = new int[3,3];
1
int[,] numbers = new int[3,3];

In the above case, we create a new two-dimensional matrix of 3x3 size. It means that it can hold 9 integer numbers. We can initialize a 2D array using the following syntax:

main.cs

main.cs

copy
datatype [,] arrayName = {
{ element1, element2, ... },
{ element1, element2, ...},
...
};
12345
datatype [,] arrayName = { { element1, element2, ... }, { element1, element2, ...}, ... };

Consider the example with real values:

main.cs

main.cs

copy
using System;

namespace ConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
int[,] numbers = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};

// Displaying the array
foreach (int number in numbers)
{
Console.Write(number + " ");
}
}
}
}
12345678910111213141516171819202122
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[,] numbers = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; // Displaying the array foreach (int number in numbers) { Console.Write(number + " "); } } } }

The above is a 3x4 matrix and it can store 12 elements. The following illustration shows a 3x3 matrix in a visualized form:

Indexing in multidimensional arrays is similar to the normal arrays. We simply mention the row and the column index.

main.cs

main.cs

copy
arrayName[row, column];
1
arrayName[row, column];

For example, if we want to access 6 from the numbers array (shown in the illustration), we will access the 2nd row and the 2rd column:

main.cs

main.cs

copy
using System;

namespace ConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
int[,] numbers = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 }
};

Console.WriteLine(numbers[1, 2]); // Output: 7
}
}
}
123456789101112131415161718
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[,] numbers = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }; Console.WriteLine(numbers[1, 2]); // Output: 7 } } }

Higher dimensional arrays are also possible by adding extra commas to the declaration syntax:

main.cs

main.cs

copy
int[,,] myArray3D = new int[3, 4, 5];
int[,,,] myArray4D = new int[5, 4, 9, 10];
// Similarly more complex ones are possible as well using the same pattern
123
int[,,] myArray3D = new int[3, 4, 5]; int[,,,] myArray4D = new int[5, 4, 9, 10]; // Similarly more complex ones are possible as well using the same pattern

In the above code myArray3D will have 60 elements (3x4x5), while myArray4D will have 1800 elements (5x4x9x10).

Following is how you would initialize a 3D array:

main.cs

main.cs

copy
int[,,] numbers =
{
{ {1, 2, 3}, { 4, 5, 6 }, {7, 8, 9} },
{ {10, 11, 12}, {13, 14, 15}, {16, 17, 18} },
{ {19, 20, 21}, {22, 23, 24}, {25, 26, 27} }
};
123456
int[,,] numbers = { { {1, 2, 3}, { 4, 5, 6 }, {7, 8, 9} }, { {10, 11, 12}, {13, 14, 15}, {16, 17, 18} }, { {19, 20, 21}, {22, 23, 24}, {25, 26, 27} } };
question mark

What is the correct code for accessing "Pumpkin" element from the foods array:

string[,] foods = {
{ "Apple", "Apricot", "Banana", "Grapes", "Lime" },
{ "Tomato", "Cabbage", "Carrot", "Pumpkin", "Broccoli" },
};

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 5. Kapittel 5

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

We use cookies to make your experience better!
some-alt