System.Array Is a Reference Type

   


System.Array Is a Reference Type

The System.Array class is a reference type like System.String. Consequently an array variable containing a reference is separate from the array object it is referencing. A reference type variable can be assigned different references, just as a variable of a numeric type can be assigned different numbers. So an array variable can reference different array objects at different segments in a program by assigning different references to it. Recall the state of affairs after the following statement

 decimal [] accountBalances = new  decimal[5]; 

as illustrated earlier in Figure 10.2. The accountBalances variable is referencing an array object of base type decimal and length 5. Suppose that later in the program you want accountBalances to reference another object of length 3000. This can be achieved by assigning a different reference to accountBalances:

 accountBalances = new decimal[3000]; 

As illustrated in Figure 10.5, the object containing 5 elements and the object of length 3000 are completely separate entities. After the latter new object has been assigned, the former old object becomes garbage (assuming accountBalances was the only reference to this old object, see the following Note) and is eventually disposed of, along with its five decimal values. accountBalances can now only be used to access array elements of the new object of length 3000.

Figure 10.5. Reassigning a new array object to an array variable.
graphics/10fig05.gif

Note

graphics/common.gif

Recall from our previous discussion about reference types in Chapter 6, "Types, Part I: The Simple Types" that an object can be referenced by any number of variables. An object only becomes garbage and disposed of when no variables are referencing it anymore, because it can then no longer be referenced; it is out of reach. More about garbage collection in Chapter 13, "Class Anatomy Part II: Object Creation and Garbage Collection."


Matching the Base Type Specified in the Array Variable Declaration

graphics/common.gif

Even though different array objects can be assigned to one array variable, the base type of any array object assigned to it must always match the type specified in the original array variable declaration. Consequently, any array object assigned to accountBalances must be of base type decimal.


Note

graphics/common.gif

The new keyword causes the runtime to allocate memory to hold a given number of elements of a given type. However, if the memory needed is greater than the memory available (this depends on the computer's hardware, other programs running, and other more technical matters), the runtime will throw an exception called OutOfMemoryException.


Due to the properties of reference types, it is possible to assign a reference held by one array variable to another array variable. This will let both array variables hold the same reference and cause them to reference exactly the same array object. Let's look at an example the array used here could be used in a program to store the ages of a list of people. The first line

 byte [] ages  = new byte [6]; 

creates the array variable ages and lets it reference a new array object of length six created after the assignment operator. It is now possible to write

 byte [] sameAges = ages; 

that will assign the reference of ages to sameAges, causing both array variables to reference exactly the same array object. This is illustrated in Figure 10.6.

Figure 10.6. Two array variables pointing at the same array object.
graphics/10fig06.gif

ages[0] is now exactly the same element as sameAges[0]. Consequently, the two lines

 ages[0] = 99; Console.WriteLine("Age of first person: " + sameAges[0]); 

will print the following

Age of first person: 99

Generally Speaking: Have Only One Array Variable Reference Per Array Object

graphics/common.gif

To have several array variables reference the same array object simultaneously can result in overly-complicated code and difficult-to-trace bugs, because seemingly separate variables can alter each other's values in different parts of a program. As a rule of thumb, use only one array variable reference per array object.



   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

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