An array is a collection of related types, and there are several ways to declare an array in MSIL. The underlying type of any array is always System.Array. With an array reference, you can call the methods and access the properties of System.Array.
Table 14-6 lists different syntax for defining an array.
Table 14-6. Syntax for defining arrays
Syntax | Description |
---|---|
type [] arrayname | Declares a one-dimensional array of an undetermined size. |
type [,] arrayname | Declares a two-dimensional array of an undetermined size. You can expand the array beyond two dimensions by extending the comma-delimited list. For example, "type [,,,] arrayname"defines a four-dimensional array. |
type [n] arrayname | Declares an array of n size. |
type [m,n] arrayname | Declares an array in which the size is mcolumns and n rows. |
type [][] arrayname | Declares a jagged array of an 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 to a local variable, to maintain access to the array. Alternatively, use the dup instruction to push additional references to the array on the evaluation stack, as needed. The newarr instruction takes as an argument the number of elements, which must be placed on the evaluation stack prior to the instruction call.
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 and stelem are shown in Table 14-7.
Table 14-7. Ldelem and stelem variations
Load element instructions | Store element instructions |
---|---|
ldelem | stelem |
ldelem.i1 | stelem.i1 |
ldelem.i2 | stelem.i2 |
ldelem.i4 | stelem.i4 |
ldelem.i8 | stelem.i8 |
ldelem.u1 | |
ldelem.u2 | |
ldelem.u4 | |
ldelem.u8 | |
ldelem.r4 | stelem.r4 |
ldelem.r8 | stelem.r8 |
ldelem.i | stelem.i |
ldelem.ref | stelem.ref |
The ldelem instruction requires the array reference and the element index on the evaluation stack, in that order. The stelem instruction requires the array reference, element index, and the new value, again in that order.
In the following code, an array of three strings is created. Each element is initialized to a different color name. The array elements then are 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 } } }
3.148.107.254