Arrays in C#

Arrays are data structures that store collections of data and allow access to the elements by using simple index operations.

Characterizations of the C# array are:

  1. C# arrays are zero indexed; the array index starts at zero.
  2. All of the array elements must be of the same type.
  3. Array elements can be of any type, including an array type.
  4. An array can be a single-dimensional array or a multidimensional array.
  5. Array types are reference types derived from the abstract base type System.Array.

In C#, arrays can be one dimensional, multidimensional, rectangular, variable length, or associative.

One-Dimensional Arrays

One-dimensional arrays are declared by stating their element type followed by empty square brackets, as
shown in the code:
Code Snippet
  1. int[] arr1;
  2. char[] characters = { 'a', 'b', 'c' };
  3. double[] arr3 = new double[5];
  4. string[] arr4 = new string[]{"Galactic Centre", "Great Rift", "Interstellar Dust"};
  5. Console.WriteLine(arr1.Length); /* Compile time error: unassigned variable */
  6. Console.WriteLine(characters.Length); /* 3 */
  7. Console.WriteLine(arr3.GetLength(0)); /* 5 */


Multidimensional Arrays

Multidimensional arrays can be jagged or rectangular. Jagged arrays hold reference to other arrays:

Code Snippet
  1. double [][] arr = new double [2][];
  2. arr[0] = new double [] {1.3, 2.4, 4.5, 6.6};
  3. arr[1] = new double [] {6.7, 1.1, 3.5};
  4. Console.WriteLine(arr[0].Length); /* 4 */
  5. Console.WriteLine(arr[1].Length); /* 3 */
  6. Console.WriteLine(arr.Rank); /* 1 (1 dimension) */
  7. Console.WriteLine(arr.GetLength(0)); /* 2 */
  8. Console.WriteLine(arr.GetLength(1)); /* Runtime error: Index was outside the bounds of the array */


Rectangular arrays 

Rectangular arrays are more efficient in searching and sorting.

Code Snippet
  1. static void Main(string[] args)
  2. {
  3. bool MyBool = null;
  4. int MyInt = null;
  5. // Compiler errors!
  6. // Value types cannot be set to null in C#.
  7. string MyString = null;
  8. // Strings are reference types in C#, Hence can be null.
  9. }


Variable-Length Arrays

Jagged or rectangular arrays have fixed lengths after declaration. The ArrayList class in the namespace System.Collections provides arrays of variable length, as shown in Listing.

Example of the Variable Length Arrays :
Code Snippet
  1. using System;
  2. using System.Collections;
  3. namespace Ch01
  4. {
  5.     class MainClass
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             ArrayList al = new ArrayList();
  10.             al.Add("C#");
  11.             Console.WriteLine(al.Count);       /* 1 */
  12.             Console.WriteLine(al[0]);          /* C# */
  13.         }
  14.     }
  15. }

This program will produce the output:
1
C#


Associative Arrays

Arrays can be indexed by strings if they are created by the class Hashtable in the namespace System.Collections, as shown in Listing below.

Example of the Associative Arrays
Code Snippet
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace Ch01
  5. {
  6.     class MainClass
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Hashtable htArray = new Hashtable();
  11.             htArray["1"] = "Milky way";
  12.             htArray["2"] = "Galaxy";
  13.             Console.WriteLine(htArray.Count);        /* 2 */
  14.             Console.WriteLine(htArray["1"]);         /* Milky way */
  15.             Console.WriteLine(htArray["2"]);         /* Galaxy */
  16.         }
  17.     }
  18. }


This program will produce the output:
2
Milky way
Galaxy

Post a Comment