Creating Arrays of Valuetypes


Valuetypes include all the native types as well as any structures you define. When you allocate an array of valuetypes, all the elements of the array are pre-allocated, which means that you can start using the array immediately.

To allocate and use an array of valuetypes:

  1. Type the type of the array, for example: double .

  2. Type an open square bracket [ and a close square bracket ] immediately after the type, for example: double[] .

  3. Type a space followed by a variable name to point to the array, for example: double[] prices .

  4. Type the = sign.

  5. Type new followed by the type of the array.

  6. Type a set of square brackets with the size of the array inside, for example: = new double[20] .

  7. Type a semicolon ; ( Figure 9.7 ).

    Figure 9.7 Although the value in square brackets is a literal number, it could also be a variable that stores the number. This enables you to have a number of elements that are calculated at runtime.
     double[] prices = new double[20]; 

graphics/tick.gif Tips

  • Once you allocate an array of valuetypes you can begin assigning values to each element ( Figure 9.8 ).

    Figure 9.8 C# uses square brackets for array indexes and parenthesis for function parameters. Other languages always use parentheses, which makes things a little confusing.
     double[] prices = new double[20];  prices[0] = 5.5;   prices[1] = 2.0;   prices[19] = 3.14;  
  • Arrays of valuetypes include arrays of structures. Figure 9.9 shows you how to create an array of structures and how you can address each element of the array directly without having to create individual structures manually.

    Figure 9.9 Structures work like classes except that they are not true objects; they behave more like integers, longs, etc. (they're valuetypes). A structure is basically a way of grouping fields that have related information. Because they're valuetypes, you can start using their fields without creating an object first.
     struct PersonalInfo {    public string Name;    public string EmbarrassingMoment; } void Task() {    PersonalInfo[] diary = new PersonalInfo[365];    diary[15].Name = "Some other Jose";    diary[15].EmbarrassingMoment    = "Recommending friend not to have kids"; } 
  • The array dimensions are specified inside square brackets. To address the elements of the array you refer to each element by its index number. The index can be zero through the length of the array minus one. So if the dimension is 32 the array indices are zero through 31.

  • The array dimensions are immutable. That means that once you set the dimension of the array, the system creates an array of that size and the size can't be expanded or shrunk. So if you wish to change the dimensions you would need to create a brand-new array. You would also need to copy any elements you wished to preserve from the old array to the new array ( Figure 9.10 ).

    Figure 9.10 Resizing the array can be done in two short steps. First create a second array and copy the elements from the first into it. Then point the original variable to the new array. The first array is now unreachable in memory space until the garbage collector cleans it up.
     void Task() {    PersonalInfo[] diary =    new PersonalInfo[365];    diary[15].Name = "Some other Jose";    diary[15].EmbarrassingMoment =    "Recommending friend not to have kids";  PersonalInfo[] tempdiary =   new PersonalInfo[1000];   System.Array.Copy(diary,0,tempdiary,0,   365);   diary = tempdiary;  } 
  • Arrays in .NET are instances of the class System.Array. System.Array provides a function called CreateInstance that lets you create an array in a slightly different way ( Figure 9.11 ).

    Figure 9.11 The first parameter in the CreateInstance function is a type objectthe type of the array you wish to create. The second parameter is the number of elements. The end result is an array object of the type you requested . However, the function is declared to return System.Array, so you need to cast the end result to the specific type.
     PersonalInfo[] diary = (PersonalInfo[])  System.Array.CreateInstance  (typeof(PersonalInfo),365); 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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