User Tools

Site Tools


net_array

This is an old revision of the document!


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

You can find more help in the MSDN page System.Array

net_array.1527738639.txt.gz · Last modified: 2018/05/31 03:50 by wolfgangriedmann