asp net 3.5unleashed

Page 278

In the figure, the Controls array shows the result after the following lines have executed: myControls[1] = new Button(); myControls[2] = new TextBox(); myControls[3] = myControls[2]; object.

// Two elements refer to the same

myControls[46] = new DataGrid(); myControls[48] = new ComboBox(); myControls[49] = new Button(); Common Language Specification (CLS) compliance requires that all arrays be zero-based. This allows a method written in C# to create an array and pass the array’s reference to code written in another language, such as Microsoft Visual Basic. In addition, because zero-based arrays are, by far, the most common arrays, Microsoft has spent a lot of time optimizing their performance. However, the CLR does support non-zero-based arrays even though their use is discouraged. For those of you who don’t care about performance or cross-language portability, I’ll demonstrate how to create and use non-zero-based arrays later in this section. Notice in Figure 14-1 that each array has some additional overhead information associated with it. This information contains the rank of the array (number of dimensions), the lower bounds for each dimension of the array (almost always 0), and the length of each dimension. The overhead also contains the type of each element in the array. Shortly, I’ll mention the methods that allow you to query this overhead information. So far, I’ve shown examples demonstrating how to create single-dimension arrays. When possible, you should stick with single-dimension, zero-based arrays, sometimes referred to as SZ arrays, or vectors. Vectors give the best performance because you can use specific IL instructions—such as newarr, ldelem, ldelema, ldlen, and stelem—to manipulate them. However, if you prefer to work with multidimension arrays, you can. Here are some examples of multidimension arrays: // Create a two-dimension array of Doubles. Double[,] myDoubles = new Double[10, 20];

// Create a three-dimension array of String references. String[,,] myStrings = new String[5, 3, 10]; The CLR also supports jagged arrays. Zero-based, single-dimension jagged arrays have the same performance as normal vectors. However, accessing the elements of a jagged array means that two or more array accesses must occur. Note that jagged arrays are not CLScompliant—because the CLS doesn’t allow a System.Array object to be an element of an array—and can’t be passed between code written in different programming languages. Fortunately, C# supports jagged arrays. Here are some examples of how to create an array of polygons, where each polygon consists of an array of Point instances: // Create a single-dimension array of Point arrays. Point[][] myPolygons = new Point[3][];

// myPolygons[0] refers to an array of 10 Point instances. myPolygons[0] = new Point[10];

// myPolygons[1] refers to an array of 20 Point instances.


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.