Arrays

Array data structures store multiple variables of the same type.

The syntax for declaring an array follows:

type[ ] arrayName;

Arrays are categorized as one-dimensional, multi-dimensional, or jagged. In the creation of an array, its number of dimensions and their length are set. The examples below declare different types of arrays in different ways:

// 1-dimensional array
int[] arrayOne = new int[12];

// Set values and declare
int[] arrayTwo = new int[] { 5, 4, 8, 6, 1 };

// Another way to set values and declare
int[] arrayThree = { 5, 6, 7, 8, 9, 10 };

// 2-dimensional
int[,] arrayTwoD = new int[7, 4];

// Multi-dimensional
int[,] arrayMulTd = { { 5, 6, 7 }, { 8, 9, 10 } };

// Jagged array
int[][] arrayJgD = new int[9][];

// Set values for arrayJgD
arrayJgD[0] = new int[22] { 3, 4, 5, 6 };

During the life of the instance, these values cannot be altered. In a numeric array, default values are set to zero, and reference elements to null. Jagged arrays are arrays containing arrays, making its elements reference types. Its elements are initialized to null.

Arrays are zero indexed meaning its elements start from 0, not 1. Arrays can include any data type including arrays. The Array abstract base type, from which array types are derived, implements IEnumerable and IEnumerable<T>, which allows for the use of foreach on all C# arrays.

ACCESS

Arrays can be accessed with a property by using the syntax below:

arrayName.Method

System.Array offers a number of useful properties and methods. Review some of these properties and methods below:

  1. IsReadOnly – This property retrieves a value to determine if the array is read-only.
  2. IsFixedSize – This property retrieves a value to determine if the array is fixed in size.
  3. Rank – This property retrieves the number of dimensions of an array.
  4. SyncRoot – This property retrieves an object used for synchronizing access to the array.
  5. BinarySearch(Array, Object) – This method searches a one-dimensional array for a specified element.
  6. Clone() - This method spawns a copy of an array containing elements and references of the original, but omits the objects referenced by the array.
  7. Clear(Array, Int32, Int32) – This method sets the elements of an array to the default value of each element's type.
  8. CopyTo(Array, Int32) – This method copies all elements of the current one-dimensional array over to a specified one-dimensional array; and starts at the provided destination index. The index must be a 32-bit integer.

Review an example of property use below:

int[] IDnumbers = {4545, 7782, 3433, 2234, 7875};
int FindLength = IDnumbers.Length;

Arrays can also be accessed with the foreach statement, which iterates through each array element. Review an example of foreach usage below:

int[] IDnumbers = {8, 2, 9, 1, 4, 7, -4, -2, 0};
foreach (int i in IDnumbers)
{
	System.Console.WriteLine(i);
}

Review an example of foreach iteration through a multi-dimensional array:

int[,] IDnumbers = new int[5, 4] {{6, 66}, {2, 22}, {4, 44}};
foreach(int i in IDnumbers)
{
	Console.Write("{0}", i);
}