Example 3: Loop through Jagged array composed of multi-dimensional arrays

CSharp · Console

/Program.cs

using System;

public class Program
{
	public static void Main()
	{
		string display = string.Empty;
		
		char[][, ] charArray = new char[2][, ]{new char[, ]{{'A', 'B', 'C'}, {'D', 'E', 'F'}}, new char[, ]{{'G', 'H'}, {'I', 'J'}}, };
		
		// First for loop is used to iterate through jagged array rows
		for (int i = 0; i < charArray.Length; i++)
		{
			display += $"Element({i}): \n";
			// For each jagged array row, 2nd and 3rd for loop is used iterate through elements on that row
			for (int j = 0; j < charArray[i].GetLength(0); j++)
			{
				// Column iteration
				for (int k = 0; k < charArray[i].GetLength(1); k++)
				{
					display += charArray[i][j, k].ToString() + " ";
				}

				display += "\n";
			}
		}

		Console.WriteLine(display);
	}
}
;