Arrays

Arrays

An array is a collection of related types, and there are several ways to declare an array in MSIL. Regardless, the underlying type of any array is System.Array. Exploiting the array reference, you can call the methods and access the properties of System.Array.

Table 11-10 lists different syntaxes for defining an array.

Table 11-10: Syntax for Defining Arrays

Array Syntax

Description

type [] arrayname

Declares a one-dimensional array of an undetermined size.

type [,] arrayname

Declares a two-dimensional array of an undetermined size.

type [n] arrayname

Declares an array of n-size.

type [m,n] arrayname

Declares an array in which the size is m-columns and n-rows.

type [][] arrayname

Declares a jagged array of undetermined size.

type [m][] arrayname

Declares a jagged array of m arrays.

The newarr instruction initializes a one-dimensional array. In addition, the instruction pushes a reference to the array onto the evaluation stack. You might want to use the array for some time. Move the array reference from the evaluation stack to memory, such as a local variable, to extend to the array. The newarr instruction requires the number of elements, which should be stored on the evaluation stack.

The syntax is as follows:

  • newarr type

Elements of an array can be accessed with the ldelem or stelem instructions. The ldelem instruction loads an element of an array onto the evaluation stack; the stelem instruction stores a value from the evaluation stack into an element of an array. Both instructions have variations that depend on the type of data being manipulated. The variations of ldelem are ldelem.i1, ldelem.i2, ldelem.i4, ldelem.i8, ldelem.u1, ldelem.u2, ldelem.u4, ldelem.u8, ldelem.r4, ldelem.r8, ldelem.i, and ldelem.ref. The permutations of the stelem instruction are stelem.i1, stelemi2, stelem.i4, stelem.i8, stelem.r4, stelem.r8, stelem.i, and stelem.ref . The ldelem instruction requires, in order, the array reference and the element index. The stelem instruction requires the array reference, element index, and the new value.

In the following code, an array of three strings is created. Each element is initialized to a different color. The array elements are then displayed in a loop.

.assembly extern mscorlib {}
.assembly application {}

.namespace Donis.CSharpBook {

    .class starter {

        .method static public void Main() il managed {
            .locals init (string [] names, int32 count)
            .entrypoint
            ldc.i4.3
            newarr string
            stloc names
            ldloc names
            ldc.i4.0
            ldstr "Aqua"
            stelem.ref
            ldloc names
            ldc.i4.1
            ldstr "Violet"
            stelem.ref
            ldloc names
            ldc.i4.2
            ldstr "Orange"
            stelem.ref
            ldc.i4.0
            stloc count
loop:       ldloc names
            ldloc count
            ldelem.ref
            call void [mscorlib] System.Console::WriteLine(string)
            ldloc count
            ldc.i4.1
            add
            dup
            stloc count
            ldc.i4.3
            blt loop
            ret
        }
    }
}