An array is a special type of data type which can store fixed number of values sequentially using special syntax. It is used to store similar data types grouping as a single unit. We can access Array elements by its numeric index. The array indexes start at zero. The default value of numeric array elements is set to zero. Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.
Array Declaration
Below is an example of how to declare an array
using the runtime library:
Note |
---|
All the C# data types have an array interface equivalent in the runtime library. Example, integer 1 dimensional array is called _Int32Array. These array interfaces are declared in CNClrLib.Core.Intf namespace. |
Array Initialization
Declaring an array does not
initialize the array in the memory. When the array variable is initialized, you
can assign values to the array. An array can be initialized using it own
coClasses defined in the runtime library. The following example shows the way of
initializing an array.
n the above example, the first statement declares & initializes int type array that can store five int values. The size of the array is specified as a parameter of the CreateInstance method. The second statement also creates an instance of an object array (System.Object[] in c#). The third statement directly initializes a dynamic array of System.Guid type with a size of 5.
Accessing Array Elements
Values can also be assigned to individual
index randomly as shown below. The following example demonstrates how to assign
values to array index:
In the same way, you can retrieve values at a
particular index as shown in the example below:
Use a for loop to access the values from all the indexes of
an array by using length property of an array. The example below demonstrates
how to access array elements using for loop.
Full Example
Array and ClrObject
Each array interface including the base array interface (_Array) defined in the runtime library has a method called AsClrObject which returns a ClrObject of the array object.
Base Array Interface
The base Array Interface is the base class for all arrays. It can be of any type. The array can be Single-Dimensional or Multidimensional.
Example
Multidimensional Arrays
Multi-dimensional arrays are also called rectangular array. The example below illustrates how to declare, initialize and access multidimensional array elements.
The following example demonstrates the use of some of the methods of the Array interface.
Array Classes and Helper
There are basically 2 array classes and an array helper class defined in the CNClrLib.Host namespace. These are TClrArray, TClrObjectArray and TClrArrayHelper.
TClrArray Class
The TClrArray is a
class wrapper of the base array
interface. It has constructors which allows you to create one or mutidimensional
array of a .net type. The example below shows how to use this class.
To access the element of this array, you can use the GetValue method to retrieve the element at a specified position or SetValue method to set a value to the element at a specified position.
TClrObjectArray Class
TClrObjectArray is an array of TClrBaseObject. The array internally holds a reference to the array interface which contains the default interface (which is the ClrObject Interface) of each elements (TClrBaseObject) in the TClrObjectArray object. For more information, see TClrObjectArray.
TClrArrayHelper Class
This is a helper class of an Array. This class contians static methods which allows you to convert delphi arrays to it equivalent arrays in .net and vice versa. For more information, see TClrArrayHelper.