Team LiB
Previous Section Next Section

Arrays

An array is a collection of like data types that can be referenced by a single name. Each member of the collection is called an element or a member of the array. Elements of an array are identified by the name of the array, followed by a unique index number contained in square brackets.

Arrays are of a specific fixed size, which is determined at the time the array is declared. All arrays start with an index number of zero. The actual amount of memory that an array takes up is the number of elements times the number of bytes that the type requires. Furthermore, all arrays are stored on the heap in C# and thus are reference types. This provides a great advantage because it removes the most common cause of stack corruption by forcing arrays to be on the heap. Finally, arrays are initialized automatically to zero for all members.

The following is an example of declaring and instantiating an instance of a single-dimension array:

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

C# arrays provide a means to prevent you from writing beyond the end of the array. When this occurs, you will throw an exception of IndexOutOfRangeException.

Jagged Arrays

Some arrays contain an array of other arrays. These are called jagged arrays. Here is an example of declaring and instantiating an instance of a jagged array:

int[][] numbers = new int[5];
for(int x = 0; x < numbers.Length; x++)
{
         numbers[x] = new int[4];
}

Multidimensional Arrays

C# also allows for multidimensional arrays. These are declared in the same way as single-dimension arrays, except that you specify the number of columns and number of rows in square brackets. For example, [2,3] declares an array that has two columns, each with three rows. Here is an example of declaring and instantiating an instance of a multidimensional array:

int[,] numbers = new int[5,4]
Note 

All arrays and most collections implement the IEnumerable interface, which allows for the simple use of the foreach loop. This interface contains the MoveNext, Reset, and Current methods.

The following example demonstrates the use of arrays.

Code Example: Arrays
Start example
using System;

namespace Client.Chapter_3___Structs__Enums__Arrays_and_Collections
{
      class Arrays1
      {
            static void Main(string[] args)
            {
                  //Note the location of the brackets indicating this is an
                  //array
                  string[] MyStaticArray = { "Hello", "World" };
                  //Creates an array with 5 members
                  int[] MyIntArray = new int[5];
                    int i, z;
                    for (int x = 0; x < 5; x++)
                    {
                         MyIntArray[x] = 0;
                    }
                  //An array of arrays, or jagged array
                  int[][][] MyIntArray2 = new int[3][][];
                  for (i = 0; i < 5; i++)
                       MyIntArray2[i] = new int[4][];
                  for (z = 0; z < 10; z++)
                       MyIntArray2[i][z] = new int[5];
            }
      }
}
using System;

namespace Client.Chapter_3___Structs__Enums__Arrays_and_Classes
{
      class Arrays2
      {
            static void Main(string[] args)
            {
                  //Multidimensional array
                  int[,] MyIntArray = new int[5, 5];
                  for (int x = 0, y = 0; x < 5; x++, y++)
                  {
                        MyIntArray[x, y] = 0;
                  }
            }
      }
}
End example

Team LiB
Previous Section Next Section