in C#. Let's see multidimensional arrays in this chapter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Program
{
static void Main()
{
int[,] Num2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } }; // Two dimensional array..
Console.WriteLine(Num2D[0, 0]); // 1 : 0th index and 0th element
Console.WriteLine(Num2D[0, 1]); // 2 : 0th index and 1st element
Console.WriteLine(Num2D[1, 0]); // 3 : 1st index and 0th element
Console.WriteLine(Num2D[1, 1]); // 4 : 1st index and 1st element
Console.WriteLine(Num2D[2, 0]); // 5 : 2nd index and 0th element
Console.WriteLine(Num2D[2, 1]); // 6 : 2nd index and 1st element
Console.WriteLine(Num2D[3, 0]); // 7 : 3rd index and 0th element
Console.WriteLine(Num2D[3, 1]); // 8 : 3rd index and 1st element
}
}
}
In the above code, we have a
2-dimensional array without fixed size.
Console prints the following lines with respect to their indexes,
Two-dimensional arrays with fixed size for Chars
We can also declare the size of the array initially like below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Program
{
static void Main()
{
char[,] Alp2D = new char[2, 2] { { 'a', 'b' }, { 'c', 'd' } }; // Two dimensional array with fixed size..
Console.WriteLine(Alp2D[0, 0]); // a : 0th index and 0th element
Console.WriteLine(Alp2D[0, 1]); // b : 0th index and 1st element
Console.WriteLine(Alp2D[1, 0]); // c : 1st index and 0th element
Console.WriteLine(Alp2D[1, 1]); // d : 1st index and 1st element
}
}
}
Note: please read the comments in the code.
In the above code, we have a
2-dimensional array of
characters with fixed size. This will prints your characters based on their index and position,
Two-dimensional arrays with fixed size for Strings
Below code explains the declaration of
2-dimensional arrays of strings ,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Program
{
static void Main()
{
string[,] Fruits2D = new string[3, 2] { { "Apple", "Banana" }, { "Mango", "Orange" }, { "Pineapple", "Strawberry" } }; // Two dimensional array with fixed size..
Console.WriteLine(Fruits2D[0, 0]); // Apple : 0th index and 0th element
Console.WriteLine(Fruits2D[1, 1]); // Orange : 1st index and 1st element
Console.WriteLine(Fruits2D[2, 0]); // Pineapple : 2nd index and 0th element
Console.WriteLine(Fruits2D[2, 1]); // Strawberry : 2nd index and 1st element
}
}
}
Note: please read the comments in the code.
In the above code, we have a
2-dimensional array of fruits (strings) with size
3,2
. This will print the
strings in the Console based on the given index as following,
Three Dimensional Arrays
Similar to the above
2-dimensional arrays, we can have
3-dimensional arrays in
C#. Let's have an example code which explains the
3-dimensional arrays,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Program
{
static void Main()
{
char[, ,] Alp3D = new char[2, 2, 2] { { { 'a', 'b' }, { 'c', 'd' } }, { { 'e', 'f' }, { 'g', 'h' } } }; // Two dimensional array with fixed size..
Console.WriteLine(Alp3D[0, 0, 0]); // a : 0th index and 0th element and 0th sub element
Console.WriteLine(Alp3D[1, 1, 1]); // h : 1st index and 1st element and 1st sub element
Console.WriteLine(Alp3D[1, 0, 1]); // f : 1st index and 0th element and 1st sub element
Console.WriteLine(Alp3D[0, 1, 1]); // d : 0th index and 1st element and 1st sub element
}
}
}
Note: please read the comments in the code.
Observe the above code carefully as it is little complicated to understand. We have three dimensions in the array with fixed size of 2,2,2
. This code will prints the characters with respect to their root,
Three-dimensional arrays with fixed size for Strings
Now let's try to create a
3-dimensional array with some strings. Take a look at the below code of
3-dimensional array with some fruit names,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Man
{
class Program
{
static void Main()
{
string[, ,] Fruits3D = new string[2, 2, 2] { { { "Apple", "Banana" }, { "Orange", "Mango" } }, { { "Lemon", "Strawberry" }, { "Pineapple", "Grape" } } }; // Two dimensional array with fixed size..
Console.WriteLine(Fruits3D[0, 0, 0]); // Apple : 0th index and 0th element and 0th sub element
Console.WriteLine(Fruits3D[1, 1, 1]); // Grape : 1st index and 1st element and 1st sub element
Console.WriteLine(Fruits3D[1, 0, 1]); // Strawberry : 1st index and 0th element and 1st sub element
Console.WriteLine(Fruits3D[0, 1, 1]); // Mango : 0th index and 1st element and 1st sub element
}
}
}
Note: please read the comments in the code.
Above code is similar to the before one and only change is using
string in the place of
char. This will prints the following lines in your
Console,
In this article, we clearly seen about 2-dimensional and 3-dimensional arrays in C# programming. Hope you understand.
Krishna.