Example 1: Jagged Array with 3 rows of different lengths

CSharp · Console

/Program.cs

using System;
					
public class Program
{
	public static void Main()
	{
		// array consists of three rows
		int[][] intArray = new int[3][];
		
		// Assign values to the elements of a jagged array. Jagged array is composed of three separate single-dimensional arrays—one per row
		intArray[0] = new int[] { 1, 2 };
		intArray[1] = new int[] { 3, 4, 5 };
		intArray[2] = new int[] { 6, 7, 8, 9 };
		
		// Alternatively, array initializer syntax
		int[][] intArray1 = { new[] { 1, 2 }, new[] { 3, 4, 5 }, new[] { 6, 7, 8, 9 } };
		
		Console.WriteLine("\n**********Access each element of the arrays**********\n");
		Console.WriteLine(intArray[0][0]); // return 1
		Console.WriteLine(intArray[1][2]); // return 5
		Console.WriteLine(intArray[2][1]); // return 7
		
		Console.WriteLine("\n**********Access a value of an indexed array element and then assign a value to it with assignment operator (=)**********\n");
		intArray[0][0] = 10;
		intArray[1][2] = 50;
		intArray[2][1] = 70;
		
		Console.WriteLine(intArray[0][0]);
		Console.WriteLine(intArray[1][2]);
		Console.WriteLine(intArray[2][1]);

	}
}
;