.Net Runtime Library for Delphi
Close
How to use Array

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:

Delphi
var intArray: _Int32Array; // can store int values boolArray: _BooleanArray; // can store boolean values stringArray: _StringArray; // can store string values doubleArray:_DoubleArray; // can store double values byteArray: _ByteArray; // can store byte values byte2DimArray: _Byte2DimArray; // can store 2 dimensional byte values objectArray: _ObjectArray; // can store object values baseArray: _Array; // can store any array type.







 
System_CAPS_note 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.

Delphi
// defining an integer array with size 5. add values later on intArray = CoInt32Array.CreateInstance(5); // defining an object array with size 5. objectArray = CoObjectArray.CreateInstance(5); // defining array of System.Guid type with size 5. baseArray = CoArray.CreateInstance('System.Guid', 5)






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:

Delphi
  intArray[0] := 10; intArray[1] := 20; intArray[2] := 30; intArray[3] := 40; intArray[4] := 50;




In the same way, you can retrieve values at a particular index as shown in the example below:

Delphi
  intArray[0]; //returns 10 intArray[2]; //returns 30

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.

Delphi
for i := 0 to intArray.Length - 1 do Console.WriteLine_8(intArray[i]);
   

 

Full Example

Delphi
program ArrayApplication; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Core; var Console: _Console; intArray: _Int32Array; i:Integer; begin Console := CoConsole.CreateInstance; //intArray is an array of 10 integers intArray := CoInt32Array.CreateInstance(10); //initialize elements of array intArray for I := 0 to 9 do intArray[I] := i + 100; Console.WriteLine_15('Array Length Before Resize: {0} ', intArray.Length); //Resize the array from size of 10 to 11 intArray.Resize(11); intArray[10] := 10 + 100; Console.WriteLine_15('Array Length After Resize: {0} ', intArray.Length); //output each array element's value for I := 0 to intArray.Length - 1 do Console.WriteLine_17('Element[{0}] = {1}', I, intArray[I]); Console.ReadKey; end. //When the above code is compiled and executed, it produces the following result: //Array Length Before Resize: 10 //Array Length After Resize: 11 //Element[0] = 100 //Element[1] = 101 //Element[2] = 102 //Element[3] = 103 //Element[4] = 104 //Element[5] = 105 //Element[6] = 106 //Element[7] = 107 //Element[8] = 108 //Element[9] = 109 //Element[10] = 110














































 

Array and ClrObjectEach 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

Delphi
program BaseArrayApp; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Core; var Console: _Console; baseArray: _Array; i:Integer; begin Console := CoConsole.CreateInstance; //Initialize the Array interface of Guid type with a size of 5 baseArray := CoArray.CreateInstance('System.Guid', 5); //Assigning values to the array index baseArray.SetValue('4F5C2B75-C407-46B6-A0CB-5DCD8CD708E9', 0); baseArray.SetValue('306A5906-E576-4C12-8010-B4E88FB0EE8E', 1); baseArray.SetValue('D0F6474D-E7BA-4392-90D3-96F8AA4E7EF1', 2); baseArray.SetValue('1448A19F-8F8A-460C-A96A-5A51FD349341', 3); baseArray.SetValue('17973E5C-3BC7-4B3D-AEE5-76EB33B87A47', 4); //Accessing base Array elements baseArray.GetValue(0); //returns '4F5C2B75-C407-46B6-A0CB-5DCD8CD708E9' baseArray.GetValue(2); //returns 'D0F6474D-E7BA-4392-90D3-96F8AA4E7EF1' //Accessing base Array elements using for loop for i := 0 to baseArray.Length - 1 do Console.WriteLine_12(baseArray.GetValue(i)); end. //Output //4f5c2b75-c407-46b6-a0cb-5dcd8cd708e9 //306a5906-e576-4c12-8010-b4e88fb0ee8e //d0f6474d-e7ba-4392-90d3-96f8aa4e7ef1 //1448a19f-8f8a-460c-a96a-5a51fd349341 //17973e5c-3bc7-4b3d-aee5-76eb33b87a47



































 

Multidimensional Arrays

Multi-dimensional arrays are also called rectangular array. The example below illustrates how to declare, initialize and access multidimensional array elements.

Delphi
program MultDimensionalArray; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Host.Helper, CNClrLib.Core; var Console: _Console; o2DimArray: _Array; o3DimArray: _Array; o4DimArray: _Array; i, j, k, l:Integer; intVal: Integer; begin Console := CoConsole.CreateInstance; //2-dimensional array of string: initialize with Values and access array elements //============================================================================== o2DimArray := CoArray.CreateInstance('System.String', 1, 2); o2DimArray.SetValue_8('test', 0, 0); o2DimArray.SetValue_8('test2', 0, 1); Console.WriteLine_14('Loop through 2-dimensional array'); for I := 0 to o2DimArray.GetLength(0) - 1 do for J := 0 to o2DimArray.GetLength(1) - 1 do Console.WriteLine_19('Element[{0}][{1}] = {2}', I, J, o2DimArray.GetValue_4(I, J)); //3-dimensional array of string: initialize with Values and access array elements //============================================================================== o3DimArray := CoArray.CreateInstance('System.String', 1, 2, 3); o3DimArray.SetValue_12('test', 0, 0, 0); o3DimArray.SetValue_12('test2', 0, 0, 1); o3DimArray.SetValue_12('test3', 0, 0, 2); o3DimArray.SetValue_12('test4', 0, 1, 0); o3DimArray.SetValue_12('test5', 0, 1, 1); o3DimArray.SetValue_12('test6', 0, 1, 2); Console.WriteLine_14('Loop through 3-dimensional array'); for I := 0 to o3DimArray.GetLength(0) - 1 do for J := 0 to o3DimArray.GetLength(1) - 1 do for K := 0 to o3DimArray.GetLength(2) - 1 do Console.WriteLine_23('Element[{0}][{1}][{2}] = {3}', TClrArrayHelper.ToObjectArray([I, J, K, o3DimArray.GetValue_6(I, J, K)])); //4-dimensional array of string: initialize with Values and access array elements //The TClrArrayHelper.ToInt32Array is used to convert delphi integer array to //.Net Integer Array //============================================================================== o4DimArray := CoArray.CreateInstance('System.String', TClrArrayHelper.ToInt32Array([1, 2, 3, 4])); o4DimArray.SetValue_4('test', TClrArrayHelper.ToInt32Array([0, 0, 0, 0])); o4DimArray.SetValue_4('test2', TClrArrayHelper.ToInt32Array([0, 0, 0, 1])); o4DimArray.SetValue_4('test3', TClrArrayHelper.ToInt32Array([0, 0, 0, 2])); o4DimArray.SetValue_4('test4', TClrArrayHelper.ToInt32Array([0, 0, 0, 3])); o4DimArray.SetValue_4('test5', TClrArrayHelper.ToInt32Array([0, 0, 1, 0])); o4DimArray.SetValue_4('test6', TClrArrayHelper.ToInt32Array([0, 0, 1, 1])); o4DimArray.SetValue_4('test7', TClrArrayHelper.ToInt32Array([0, 0, 1, 2])); o4DimArray.SetValue_4('test8', TClrArrayHelper.ToInt32Array([0, 0, 1, 3])); o4DimArray.SetValue_4('test9', TClrArrayHelper.ToInt32Array([0, 0, 2, 0])); o4DimArray.SetValue_4('test10', TClrArrayHelper.ToInt32Array([0, 0, 2, 1])); o4DimArray.SetValue_4('test11', TClrArrayHelper.ToInt32Array([0, 0, 2, 2])); o4DimArray.SetValue_4('test12', TClrArrayHelper.ToInt32Array([0, 0, 2, 3])); o4DimArray.SetValue_4('test13', TClrArrayHelper.ToInt32Array([0, 1, 0, 0])); o4DimArray.SetValue_4('test14', TClrArrayHelper.ToInt32Array([0, 1, 0, 1])); o4DimArray.SetValue_4('test15', TClrArrayHelper.ToInt32Array([0, 1, 0, 2])); o4DimArray.SetValue_4('test16', TClrArrayHelper.ToInt32Array([0, 1, 0, 3])); o4DimArray.SetValue_4('test17', TClrArrayHelper.ToInt32Array([0, 1, 1, 0])); o4DimArray.SetValue_4('test18', TClrArrayHelper.ToInt32Array([0, 1, 1, 1])); o4DimArray.SetValue_4('test19', TClrArrayHelper.ToInt32Array([0, 1, 1, 2])); o4DimArray.SetValue_4('test20', TClrArrayHelper.ToInt32Array([0, 1, 1, 3])); o4DimArray.SetValue_4('test21', TClrArrayHelper.ToInt32Array([0, 1, 2, 0])); o4DimArray.SetValue_4('test22', TClrArrayHelper.ToInt32Array([0, 1, 2, 1])); o4DimArray.SetValue_4('test23', TClrArrayHelper.ToInt32Array([0, 1, 2, 2])); o4DimArray.SetValue_4('test24', TClrArrayHelper.ToInt32Array([0, 1, 2, 3])); Console.WriteLine_14('Loop through 4-dimensional array'); for I := 0 to o4DimArray.GetLength(0) - 1 do for J := 0 to o4DimArray.GetLength(1) - 1 do for K := 0 to o4DimArray.GetLength(2) - 1 do for L := 0 to o4DimArray.GetLength(3) - 1 do Console.WriteLine_23('Element[{0}][{1}][{2}][{3}] = {4}', TClrArrayHelper.ToObjectArray([I, J, K, L, o4DimArray.GetValue_2( TClrArrayHelper.ToInt32Array([I, J, K, L]))])); Console.ReadKey; end. //Output //Loop through 2-dimensional array //Element[0][0] = test //Element[0][1] = test2 //Loop through 3-dimensional array //Element[0][0][0] = test //Element[0][0][1] = test2 //Element[0][0][2] = test3 //Element[0][1][0] = test4 //Element[0][1][1] = test5 //Element[0][1][2] = test6 //Loop through 4-dimensional array //Element[0][0][0][0] = test //Element[0][0][0][1] = test2 //Element[0][0][0][2] = test3 //Element[0][0][0][3] = test4 //Element[0][0][1][0] = test5 //Element[0][0][1][1] = test6 //Element[0][0][1][2] = test7 //Element[0][0][1][3] = test8 //Element[0][0][2][0] = test9 //Element[0][0][2][1] = test10 //Element[0][0][2][2] = test11 //Element[0][0][2][3] = test12 //Element[0][1][0][0] = test13 //Element[0][1][0][1] = test14 //Element[0][1][0][2] = test15 //Element[0][1][0][3] = test16 //Element[0][1][1][0] = test17 //Element[0][1][1][1] = test18 //Element[0][1][1][2] = test19 //Element[0][1][1][3] = test20 //Element[0][1][2][0] = test21 //Element[0][1][2][1] = test22 //Element[0][1][2][2] = test23 //Element[0][1][2][3] = test24































































































































The following example demonstrates the use of some of the methods of the Array interface.

Delphi
program UseArrayMethods {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Core; var Console: _Console; intArray: _Int32Array; I: Integer; begin Console := CoConsole.CreateInstance; intArray := CoInt32Array.CreateInstance(8); intArray[0] := 34; intArray[1] := 72; intArray[2] := 13; intArray[3] := 13; intArray[4] := 44; intArray[5] := 25; intArray[6] := 30; intArray[7] := 10; Console.Write_22('Original Array: '); for I := 0 to intArray.Length - 1 do Console.Write_22(IntToStr(intArray[I]) + ' '); Console.WriteLine; // reverse the array intArray.Reverse; Console.Write_22('Reversed Array: '); for I := 0 to intArray.Length - 1 do Console.Write_22(IntToStr(intArray[I]) + ' '); Console.WriteLine; //sort the array intArray.Sort; Console.Write_22('Reversed Array: '); for I := 0 to intArray.Length - 1 do Console.Write_22(IntToStr(intArray[I]) + ' '); Console.WriteLine; Console.ReadKey; end. //Output //Original Array: 34 72 13 13 44 25 30 10 //Reversed Array: 10 30 25 44 13 13 72 34 //Reversed Array: 10 13 13 25 30 34 44 72


















































 

 

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.

Delphi
var cArray: TClrArray; begin //Create 1 dimensional Array of Integer type of size 5 cArray := TClrArray.Create('System.Integer', 5); //Create 2 dimensional Array of Integer type of dimensions 5 and 3 cArray := TClrArray.Create(TClrAssembly.GetType('System.Integer'), 5, 3); //Create 4 dimensional Array of Integer type of dimensions 5, 3 and 2 cArray := TClrArray.Create(TClrAssembly.GetType('System.Integer'), TClrArrayHelper.ToInt32Array([5, 3, 2])); end.











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.