Question

In studying the internals of the CLI, I notice that it is possible for type signatures to specify array bounds (at compile time.) For instance, instead of having:

Byte[,]

We can have:

Byte[1...100,1...]

If I am not mistaken it is not possible to actually declare types like the latter in either C# or C++/CLI. Furthermore, ECMA-335 indicates:

The VES creates one array type for each distinguishable array type. In general, array types are only distinguished by the type of their elements and their rank.

The Common Language Specification (CLS) imposes a number of restrictions, including:

Only the fact that an item is an array and the element type of the array shall be required to distinguish between overloads.

So it seems that support for compile-time array bounds is present at the lowest levels of the CLI but not elsewhere. This leads me to wonder:

  • When would a type signature including this information ever be created/encountered in practice? Are there .NET languages that do emit this? EDIT: There are languages that probably do use one-based arrays normally - but do they encode this into the type signatures?

  • Where does the CLI actually use this information if it is present? (This in particular is of interest to me as I'm designing a CLI implementation.)

As far as I can see, any array is created either using the newarr bytecode instruction for vectors, or Array.CreateInstance for the general case. Neither of these mechanisms accept a type signature for the array itself, so creation it seems cannot use this information.

Theoretically, this information could be useful to optimize array access by inlining efficient machine code to compute the address. However, ldelem and friends only access simple vectors; multi-dimensional array access requires a method call, which makes me think such an optimization probably doesn't take place.

I have to think there is some purpose for this mechanism, in order for it to have made it into an international standard, but I'm not really seeing it.

Was it helpful?

Solution

This information only appears in the signature of the array type, it is not available at runtime without accessing the array object members. This is important for languages that consider arrays with different bounds to be distinct types, the Pascal language is an example of that. It treats a variable of type array[1..10] of integer as distinct and incompatible with a variable of type array[1..11] of integer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top