Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Arrays on .NET 1.X

Back to Forum: Tech Off
  • User profile image
    raptor3676

    Hi everybody

    In the current version of the framework all arrays no matter what type inherits from the abstract System.Array base class, only compilers are allowed to do so.

    But with all of this noise about the generic support on .NET 2.0, it makes me wonder how compilers avoid the overhead caused by box/unboxing (caused by using object as the type in the base class)?

    Or is this overhead inevitable in the current implementation and something we all are already used to?

  • User profile image
    Sven Groot

    Arrays themselves are reference types. Members of arrays can be value or reference types. No boxing/unboxing is necessary as far as I know.

  • User profile image
    footballism

      when the element type of an array is value type, this array's individual element is still stored in the stack, not heap, so when you reference to the individual element of an array, boxing/unboxing will never be involved:P

  • User profile image
    TomasDeml

    footballism wrote:
      when the element type of an array is value type, this array's individual element is still stored in the stack, not heap, so when you reference to the individual element of an array, boxing/unboxing will never be involved:P


    In the book Applied Microsoft .NET Framework Programming by Jeffrey Richter, I read:

    Book wrote:

    All array types are implicitly derived from System.Array, which itself is derived from System.Object. This means that all arrays are always reference types which are allocated on the managed heap, and your app's variable contains a reference to the array and not the array itself.
    Generic Forum ImageThe following code makes this point clearer:
    Int32[] myIntegers;            // Declares a reference to an array
    myIntegers = new Int32[100]; // Create array of 100 Int32s
    On the first line, myIntegers is a variable that is capable of pointing to a single-dimensioned array of Int32s. Initially, myIntegers will be set to null since I have not allocated an array. The second line shown above allocates an array of 100 Int32 values; all of the Int32s are initialized to 0. Even though Int32s are value types, the memory block large enough to hold these values is allocated from the managed heap. The memory block contains 100 unboxed Int32 values. The address of this memory block is returned and saved in the variable myIntegers.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.