Arrays

I l @ ve RuBoard

In C#, arrays are a special entity. All arrays implicitly inherit from the System.Array type. The System.Array base class provides various methods used during the manipulation of arrays. Arrays are also index checked; that is, any attempt to access an invalid index, such as an index out of range, will generate an exception. In C and C++, simple arrays were not ranged checked and it was possible to overwrite the stack space or the heap; not so in C#.

The declaration of an array might seem slightly odd at first, but when you analyze the syntax, it actually makes much more sense than the C and C++ declaration for arrays.

The following syntax is used to allocate an array of Rank 1.

 array-type[] var = new array-type[size] 

Notice that the array brackets are next to the array type and not the variable name . This syntax actually makes more sense than placing the brackets next to the variable name. After all, the array is the type and type declarations precede the variable name.

Arrays in C# use zero-based indexing. Languages, such as COBOL and Visual Basic, use one based indexing, although Visual Basic allows you to define the default array indexing base value. Listing 2.1.18 makes use of a single dimension array and some of the methods the System.Array type provides.

Listing 2.1.18 Single Dimension Array
 1: //File    :part02_17.cs  2: //Author  :Richard L. Weeks  3: //Purpose :Demonstrate C# arrays  4:  5: using System;  6:  7: public class ArrayTest {  8:  9:     public static void Main( ) { 10: 11:        //Declare a single dim array of ints 12:        int[] array_1 = new int[5]; 13: 14:        //Fill the array 15:        for( int i = array_1.GetLowerBound(0); i <= array_1.GetUpperBound(0); i++) 16:           array_1[i] = i+1; 17: 18:        //Display the contents of the array 19:        for( int j = array_1.GetLowerBound(0); j <= array_1.GetUpperBound(0); j++) 20:           Console.WriteLine("array_1[{0} ] = {1} ", j, array_1[j]); 21: 22:        Console.WriteLine("\n****** Phase II ******\n"); 23: 24:        //Declare an array and initialize the values 25:        int[] array_2 = new int[] { 25, 10, 4, 7, 15, 2, 1 } ; 26: 27:        //Sort the array 28:        System.Array.Sort( array_2 ); 29: 30:        //Display the sorted values 31:        for( int k = array_2.GetLowerBound(0); k <= array_2.GetUpperBound(0); k++) 32:           Console.WriteLine("array_2[{0} ] = {1} ", k, array_2[k] ); 33: 34: 35:     } 36: } 37: 

Listing 2.1.18 makes use of the System.Array methods GetLowerBound and GetUpperBound . The method GetLowerBound takes an integer argument that specifies the Rank for which to get the lower index value. GetUpperBound also takes an integer argument that specifies the Rank to get the largest index value. Notice that the for statement on lines 15, 19, and 31 all make use of these methods.

Lines 12 through 16 declare an array and use a for statement to initialize the values. On line 25, the declaration and initialization is done with a single statement.

The System.Array class also provides the static method Sort . The Sort method can be used to sort intrinsic types or any type that implements the IComparable interface. Interface implementation will be covered later in Chapter 2.2, "Advanced C#."

C# arrays are not restricted to a single dimension. Declaring arrays of rank greater than one merely requires specifying the lengths of each rank. It is also important to note that arrays do not have to be rectangular. Each rank can have a different upper bound (see Listing 2.1.19).

Listing 2.1.19 Multi-Dimensional Arrays
 1: //File    :part02_18.cs  2: //Author  :Richard L. Weeks  3: //Purpose :Arrays with a Rank greater than 1  4:  5:  6: using System;  7:  8: public class ArrayTest {  9:     public static void Main( ) { 10: 11:         int[,] grid = new int[3,3] { {1,2,3} , {4,5,6} , {7,8,9}  } ; 12: 13:     //Display the contents of the grid 14:         for( int i = 0; i < 3; i++ ) { 15:             for( int j = 0; j < 3; j++ ) { 16:                 Console.Write("{0}  ", grid[i,j] ); 17:             } 18:             Console.WriteLine(""); 19:         } 20:     } 21: } 

The declaration for the array variable grid on line 11 follows the same syntax and initialization as an array of rank 1. The only difference in the number of dimensions is now 2; the rank of the array is 2. Notice that the indexers for the array are separated with a comma. This syntax will be familiar to Visual Basic programmers, but to C and C++ developers, the syntax is different from what you are use to seeing.

Listing 2.1.19 also makes use of an initialization list when declaring the grid array.

I l @ ve RuBoard


C# and the .NET Framework. The C++ Perspective
C# and the .NET Framework
ISBN: 067232153X
EAN: 2147483647
Year: 2001
Pages: 204

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net