Arrays (C# vs Java)

Arrays are ordered collections of items of the same data type that are accessed using the array name in conjunction with the offset from the start of the array of the desired item. There are some important differences in how arrays are declared and used in C# compared to Java.

The One-Dimensional Array

A one-dimensional array stores a fixed number of items in a linear fashion, requiring just a single index value to identify any one item. In C#, the square brackets in the array declaration must follow the data type, and cannot be placed after the variable name as is permitted in Java. Thus, an array of type integers is declared using the following syntax:

int[] arr1;

The following declaration is invalid in C#:

//int arr2[];  //compile error

Once you have declared an array, you use the new keyword to set its size, just as in Java. The following declares the array reference:

int[] arr;
arr = new int[5];  // create a 5 element integer array

You then access elements in a one-dimensional array using identical syntax to Java. C# array indices are also zero-based. The following accesses the last element of the previous array:

System.Console.WriteLine(arr[4]);  // access the 5th element


C# array elements can be initialized at creation using the same syntax as in Java:

int[] arr2Lines;
arr2Lines = new int[5] {1, 2, 3, 4, 5};

Unlike Java, the number of C# initializers must match the array size exactly. You can use this feature to declare and initialize a C# array in a single line:

int[] arr1Line = {1, 2, 3, 4, 5};

This syntax creates an array of size equal to the number of initializers.

Initializing in a Program Loop

The other way to initialize an array in C# is to use the for loop. The following loop sets each element of an array to zero:

int[] TaxRates = new int[5];

for (int i=0; i<TaxRates.Length; i++)
    TaxRates[i] = 0;

Jagged Arrays

Both C# and Java support creating jagged, or non-rectangular, arrays in which each row contains a different number of columns. For instance, the following jagged array has four entries in the first row, and three in the second:

int[][] jaggedArray = new int[2][];
jaggedArray[0] = new int[4];
jaggedArray[1] = new int[3];

Multi-Dimensional Arrays

With C#, you can create regular multi-dimensional arrays that are like a matrix of values of the same type. While both Java and C# support jagged arrays, C# also supports multi-dimensional arrays or arrays of arrays.

Declare a multi-dimensional rectangular array using following syntax:

int[,] arr2D;      // declare the array reference 
float[,,,] arr4D;  // declare the array reference

Once declared, you allocate memory to the array as follows:

arr2D = new int[5,4];  // allocate space for 5 x 4 integers

Elements of the array are then accessed using the following syntax:

arr2D[4,3] = 906;

Because arrays are zero-based, this line sets the element in the fifth column of the fourth row to 906.


Multi-dimensional arrays can be created, set up, and initialized in a single statement by any of the following methods:

int[,] arr4 = new int [2,3] { {1,2,3}, {4,5,6} };
int[,] arr5 = new int [,]   { {1,2,3}, {4,5,6} };
int[,] arr6 =               { {1,2,3}, {4,5,6} };

Initializing in a Program Loop

All the elements of an array can be initialized using a nested loop as shown here:

int[,] arr7 = new int[5,4];

for(int i=0; i<5; i++)
    for(int j=0; j<4; j++)
        arr7[i,j] = 0;  // initialize each element to zero

The System.Array Class

In the .NET Framework, arrays are implemented as instances of the Array class. This class provides several useful methods, such as Sort and Reverse.

The following example demonstrates how easy these methods are to work with. First, you reverse the elements of an array using the Reverse method, and then you sort them with the Sort method:

class ArrayMethods
    static void Main()
        // Create a string array of size 5: 
        string[] employeeNames = new string[5];

        // Read 5 employee names from user:
        System.Console.WriteLine("Enter five employee names:");
        for(int i=0; i<employeeNames.Length; i++)
            employeeNames[i]= System.Console.ReadLine();

        // Print the array in original order:
        System.Console.WriteLine("\nArray in Original Order:");
        foreach(string employeeName in employeeNames)
            System.Console.Write("{0}  ", employeeName);

        // Reverse the array:

        // Print the array in reverse order:
        System.Console.WriteLine("\n\nArray in Reverse Order:");
        foreach(string employeeName in employeeNames)
            System.Console.Write("{0}  ", employeeName);

        // Sort the array:

        //  Print the array in sorted order:
        System.Console.WriteLine("\n\nArray in Sorted Order:");
        foreach(string employeeName in employeeNames)
            System.Console.Write("{0}  ", employeeName);


Enter five employee names:






Array in Original Order:

Luca Angie Brian Kent Beatriz

Array in Reverse Order:

Beatriz Kent Brian Angie Luca

Array in Sorted Order:

Angie Beatriz Brian Kent Luca

See Also


C# Programming Guide


Arrays (C# Programming Guide)

Other Resources

The C# Programming Language for Java Developers