< Day Day Up > |
C#, like most programming languages, provides the array data structure as a way to collect and manipulate values of the same type. Unlike other languages, C# provides three types of arrays. One is implemented as an ArrayList object; another as a generic List object; and a third is derived from the System.Array class. The latter, which is discussed here, has the traditional characteristics most programmers associate with an array. The ArrayList is a more flexible object that includes special methods to insert and delete elements as well as dynamically resize itself. The List is a type-safe version of the ArrayList that was introduced with .NET 2.0 and may eventually replace the ArrayList. The List and ArrayList are discussed in Chapter 4, along with other Collection classes. Before looking at the details of creating and using an array, you should be familiar with its general features:
Declaring and Creating an ArraySyntax: <type> identifier [ ] = new <type> [n] [{ initializer list}] Example: int[] myRating; // declares an array myRating = new int[5]; // creates array and allocates memory int[] myRating = new int[5] {3,4,7,2,8}; int[] myRating = {3,4,7,2,8}; // shorthand version // Create array containing instances of an Apparel class. Apparel myApparel = {new Apparel(), new Apparel(), new Apparel()); // Set to an enum Fabric[] enumArray = new Fabric[2]; enumArray[0] = Fabric.Cotton; // Create a 2-dimensional array with 3 rows and 2 columns int[ , ] myRatings = {{3 , 7}, {4 , 9}, {2, 6}}; The size of the array is determined from the explicit dimensions or the number of elements in the optional initializer list. If a dimension and an initializer list are both included, the number of elements in the list must match the dimension(s). If no initialization values are specified, the array elements are initialized to 0 for numeric types or null for all others. The CLR enforces bounds checking any attempt to reference an array index outside its dimensions results in an exception. Using System.Array Methods and PropertiesThe System.Array class defines a number of properties and methods that are used to query an array and manipulate its contents. Array operations include sorting, copying, searching for specific values, and clearing the contents. Table 2-10 summarizes some of the more useful members of the System.Array class.
The members are classified as static or instance. A static member is not associated with any particular array. It operates as a built-in function that takes any array as a parameter. The instance members, on the other hand, are associated with a specific instance of an array. The example shown in Listing 2-3 demonstrates how to use many of these class members. Listing 2-3. Working with Arrays Using System.Array Membersclass MyApp { static void Main() { string[] artists = {"Rembrandt", "Velazquez", "Botticelli", "Goya", "Manet","El Greco"}; // ..Sort array in ascending order Array.Sort(artists); // ..Invert the array Array.Reverse(artists); PrintArray(artists); // call method to list array int ndx = Array.IndexOf(artists,"Goya"); // ndx = 3 // ..Clone the array string[] artClone = (string[]) artists.Clone(); // Do arrays point to same address? bool eq = Object.ReferenceEquals( artClone[0],artists[0]); // true Array.Clear(artClone,0,artClone.Length); // ..Copy selected members of artists to artClone Array.Copy(artists,1,artClone,0,4); eq = Object.ReferenceEquals( artClone[0],artists[1]); // true } // List contents of Array public static void PrintArray(string[] strArray) { for ( int i = 0; i<= strArray.GetUpperBound(0); i++ ) { Console.WriteLine(strArray[i]); } } } Things to note:
|
< Day Day Up > |