Arrays

Arrays are a group of values of similar data types. These values are stored in continuous memory locations, thereby, making it easier to access and manipulate the values. Arrays in C# belong to the reference type and hence are stored on the heap. The declaration of arrays in C# follow the syntax given below:

DataType[number of elements] VariableName;

For example,

int[6] arr1;

In the above example, we have specified the number of elements the array will consist of, However, this is optional in C#. This means that the number of elements in the array can be assigned later in the program. For example,

int[] arr2;
arr2 = new int[9];

The initialization of an array can be done when it is declared or at a later stage in the program. The initialization of the array at the declaration stage is done in the following manner:

int[] arr3 = {0,1,2};

Leave a comment