Language Basics C# Book

class MainClass
{
public static void Main()
{
int[,] theArray = { { 1, 2 }, { 2, 3 }, { 3, 4 } };
Print2DArray(theArray);
}
public static void Print2DArray(int[,] arr)
{
foreach (int a in arr)
{
System.Console.WriteLine(a);
}
}
}
The output:
1
2
2
3
3
4
You can initialize and pass a new array in one step, as is shown in the following example.
class MainClass
{
public static void Main()
{
Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
}
public static void Print2DArray(int[,] arr)
{
foreach (int a in arr)
{
System.Console.WriteLine(ar);
}
}
}
The output:
1
2
3
4
5
6
7
8
In the following example, a two-dimensional array of integers is initialized and passed to the Print2DArray method.
The method displays the elements of the array.

class MainClass
{
static void Print2DArray(int[,] arr)
{
// Display the array elements.
for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
System.Console.WriteLine("Element({0},{1})={2}", i, j, arr[i, j]);
}
}
}
static void Main()
{
// Pass the array as an argument.
Print2DArray(new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } });
}
}
The output:
Element(0,0)=1
Element(0,1)=2
Element(1,0)=3
Element(1,1)=4
Element(2,0)=5
Element(2,1)=6
Element(3,0)=7
Element(3,1)=8