Creating Arrays of Reference Types


Creating arrays of reference types requires an extra step over creating an array of valuetypes. Once you create an array of reference types, you have to create each individual element before you can manipulate the elements of the array.

To create an array of objects:

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

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

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

  4. Type the equal 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 Child[5] .

  7. Type a semicolon ; .

  8. For each element in the array create a new object. For example: children[0] = new Child(); ( Figure 9.12 ).

    Figure 9.12 When you allocate an array of reference types, all you're doing is allocating a series of slots that will store the address to the actual objects; but you have to create the objects to go in the slots yourself.
     class Child {    public string name;    public void BegForNewVideoGame(string game)    {    } } void Task() {  Child[] children = new Child[5];   children[0] = new Child();   children[0].BegForNewVideoGame("Metroid");  } 

graphics/tick.gif Tips

  • As you can see, allocating an array of reference types is a two-step process. You first declare and create an array object like you would with valuetypes. Then you have to create a new object for each element in the array.

  • All the elements in the array are initialized to null when the array is first allocated.

  • Arrays of reference types are compatible with arrays of parent types. In other words, if you have a class of type Child and Child is derived from a class called HeightChallengedPerson, then an array of Child is compatible with a variable of type HeightChallengedPerson [] ( Figure 9.13 ). It follows that an array of the type System.Object would be compatible with any type of array ( Figure 9.14 ). In fact a variable of System.Object (not an array type) could also point to an entire array ( Figure 9.15 ). It's also true that if the Child class implements an interface, then the array is also compatible with an array of the interface ( Figure 9.16 ).

    Figure 9.13 Child derives from HeightChallengedPerson, therefore, an array of Child is compatible with an array of HeightChallengedPerson.
     class HeightChallengedPerson {    public string Name;    public int Age; } class Child : HeightChallengedPerson {    public void BegForNewVideoGame(string game)    {    } } void Task2() {    Child[] children = new Child[5];    children[0] = new Child();    children[0].Name = "Ralph";  HeightChallengedPerson[] hp = children;   string name = hp[0].Name;  } 
    Figure 9.14 Since object is the base class to every class, an array of Child is also compatible with an array of object.
     class Child {    public string name;    public void BegForNewVideoGame(string game)    {    } } void Task() {    Child[] children = new Child[5];    children[0] = new Child();    children[0].name = "Tommy";  object[] objChild = children;   string name = ((Child)objChild[0]).name;  } 
    Figure 9.15 The object variable can point not only to a single object but also to any type of array.
     class Child {    public string name;    public void BegForNewVideoGame(string game)    {    } } void Task() {    Child[] children = new Child[5];    children[0] = new Child();    children[0].name = "Tommy";  object objChild = children;   Child[] otherchildren = (Child[])objChild;   string name = otherchildren[0].name;  } 
    Figure 9.16 If a class implements an interface, then an array of that type is compatible with an array of the interface type.
     interface IGoodNegotiator {    void BegForNewVideoGame(string game); } class Child :  IGoodNegotiator  {    public string name;    public void BegForNewVideoGame(string game)    {    } } void Task() {    Child[] children = new Child[5];    children[0] = new Child();    children[0].name = "Tommy";  IGoodNegotiator[] negs = children;   negs[0].BegForNewVideoGame("Halo 2");  } 
  • Because arrays are instances of the type System.Array , a variable of type System.Array can point to any array object. System.Array provides a series of functions that enable you to read and write to the array ( Figure 9.17 ).

    Figure 9.17 With System.Array you can create arrays and manipulate arrays without ever having to declare a variable of a specific type of array. This lets developers create arrays on the fly at runtime as needed.
     class Child {    public string name;    public void BegForNewVideoGame(string game)    {    } } void Task() {    Child[] children = new Child[5];  System.Array arr = children;   arr.SetValue(new Child(),3);   Child tommy = (Child)arr.GetValue(3);   double[] ages = new double[4];   arr = ages;   arr.SetValue(3.5,2);   double age2 = arr.GetValue(2);  } 



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