ˇ  C# - Syntax - Collections - Array

 

- Info:

  - Array index is positive integer that starts at 0.

  - Size of the array is not part of its type unlike in C language.

    This allows you to declare an array and assign any array of same objects to it, regardless of the array's length.

  - This tutorial is based on:

    http://msdn.microsoft.com/en-us/library/aa288453(VS.71).aspx

 

- Create Array:

  - int[]     array;                         //Declared. Used to define scope.

  - int[]     array = null;                  //Declared, set to null.

  - int[]     array = new int[3];            //Declared, memory reserved, inital values.

  - int[]     array = new int[3] {0, 1, 2};  //Declared, memory reserved, elements added.

  - int[]     array = new int[ ] {0, 1, 2};  //Declared, memory reserved, elements added.

  - int[]     array =            {0, 1, 2};  //Declared, memory reserved, elements added.

 

- Create Array - Initial Values:

  - bool   [] array = new bool   [5];        //Elements set to initial value false.

  - char   [] array = new char   [5];        //Elements set to initial value 0x0000.

 

  - sbyte  [] array = new sbyte  [5];        //Elements set to initial value 0.

  - short  [] array = new short  [5];        //Elements set to initial value 0.

  - int    [] array = new int    [5];        //Elements set to initial value 0.

  - long   [] array = new long   [5];        //Elements set to initial value 0.

 

  - byte   [] array = new byte   [5];        //Elements set to initial value 0.

  - ushort [] array = new ushort [5];        //Elements set to initial value 0.

  - uint   [] array = new uint   [5];        //Elements set to initial value 0.

  - ulong  [] array = new ulong  [5];        //Elements set to initial value 0.

 

  - float  [] array = new float  [5];        //Elements set to initial value 0.0.

  - double [] array = new double [5];        //Elements set to initial value 0.0.

  - decimal[] array = new decimal[5];        //Elements set to initial value 0.0.

 

- Change Array:

  -           array = null;                  //Set to null.

  -           array = new int[3];            //Memory reserved, inital values.

  -           array = new int[ ] {0,1,2};    //Memory reserved, elements added.

  -           array = array2;                //array gets dimension & all elements from array2.

 

- Add Element:

  - array[0] = 5;                            //Add value 5 at index 0.

 

- Get Element:

  - int     element = array[0];              //Get element at index 0.

 

- Statistics:

  - int     length  = array.Length;           //Number of elements of array.