Example 2: Jagged Array with 2 rows of different lengths

CSharp · Console

/Program.cs

using System;
					
public class Program
{
	public static void Main()
	{
		// array consists of two rows
		char[][] charArray = new char[2][];
		
		// Assign values to the elements of a jagged array. Jagged array is composed of two separate single-dimensional arrays—one per row
		charArray[0] = new char[] { 'A', 'B', 'C' };
		charArray[1] = new char[] { 'D', 'E' };

		
		// Alternatively, array initializer syntax
		char[][] charArray1 = { new[] { 'A', 'B', 'C' }, new[] { 'D', 'E' } };
		
		Console.WriteLine("\n**********Access each element of the arrays**********\n");
		Console.WriteLine(charArray[0][2]); // return C
		Console.WriteLine(charArray[1][1]); // return E
		
		Console.WriteLine("\n**********Access a value of an indexed array element and then assign a value to it with assignment operator (=)**********\n");
		charArray[0][2] = 'F';
		charArray[1][1] = 'G';
		
		Console.WriteLine(charArray[0][2]);
		Console.WriteLine(charArray[1][1]); 

	}
}
;