User Tools

Site Tools


net_array

.NET Arrays

Compared with the VO and Vulcan.NET dynamic arrays, the .NET arrays are very limited.

They cannot grow, cannot shrink and cannot be sorted, and they can contain only elements of one datatype (or class). Of course, the could contain data of type object, and in an object you can store any data, but this does not changes the biggest disadvantage: they remain fixed. Of course, they are very fast.

The major use you will probably find for such arrays is that many .NET methods return an array. The best sample may be the handling of binary data in form of a array of bytes:

A static array is defined by any datatype, followed by an open and a close square bracket:

local aData as byte[]

To initialize such an array, you need to specify also the dimension, or to specify the values, casting to the right datatype:

aData := byte[]{ 10 }
aData := <byte> { 1, 2, 3, 4, 5 }

It depends on the compiler settings of your application, if the index of your array starts with 0 (like C#) or with 1 (like VO). Therefore it is better to use the foreach operator to enumerate a static array.

foreach cByte as byte in aData
  // Do something
next

Since an array is also an object, it has its own properties and methods. But the static methods of the array class are much more interesting:

nIndex := Array.IndexOf( aData, 2 )
cByte := Array.Find( aData, { a as byte => a == 2 } )
cByte := Array.Find( aData, FindByte )
 
static method FindByte( b as byte ) as logic
if b == 2
  return true
endif
return false

There are two different types of multidimensional arrays with different syntax: The jagged arrays or arrays of arrays:

local aJagged as string[][]
 
aJagged := <string[]>{ <string>{ "X#", "xsharp.info" }, <string>{ "C#", "microsoft.com" } }
System.Console.WriteLine( aJagged[0][0] )

The jagged arrays are more efficient, but have a (for xBase users) strange syntax to access the members.

local aJagged as string[][]
aJagged := <string[]>{string[]{nLen}, string[]{nLen},string[]{nLen} }

At this level, there are no strings in aJagged. Only NULL values.

And now (if zero based), it can be filled:

aJagged[02][0..nLen-1] := "Test"
 
aJagger[0][0] := "First String element in the 1st array"
aJagged[1][2] := "Hello !"
aJagged[2][nLen-1] := "Last String element in the last array"

There are also multidimensional arrays:

local aMulti as string[,]
 
aMulti := string[,]{2,2}{ <string>{ "X#", "xsharp.info" }, <string>{ "C#", "microsoft.com" } }
System.Console.WriteLine( aMulti[0,0] )

Of course you can use them also in more than 2 dimensions.

In the daily coding the Collections are much more important, because they can be resized.

But it is important to know how to deal with arrays because some methods return them or expect them as parameters, and because these are extremely efficient.

The VO dynamic arrays pay a high price for their flexibility: every member is an usual (with the known overhead at runtime and in memory) and therefore any type checking must be done at runtime by the programmers code (for the .NET arrays the compiler does these checks). But the X# runtime (ready from June 2018) introduces typed arrays, so you can have both: the flexibility of the VO array and the compiler checks of the .NET array.

You can find more help informations about .NET arrays in the MSDN page System.Array

net_array.txt · Last modified: 2018/09/09 06:20 by wolfgangriedmann