CLR notes: 13. Array

Source: Internet
Author: User
Tags array length arrays

The CLR supports one-dimensional/multidimensional/jagged arrays.

Two ways of declaring:

Array A;
A = new string[0, 1];
String[] s = new string[5];

Note that the declaration does not give the array length, because memory is not allocated at this time, and the length is specified when new.

Declaring an array as an array and like string[] with brackets, the effect is the same, but the former is more flexible, of course, the type is unsafe, there may be run-time errors.

All arrays are implicitly derived from System.Array, which derive from System.Object, so the array is a reference type.

An array of value types, when new, creates an actual object with all elements initialized to 0;

An array of reference types, when new, just creates a reference and does not create an actual object.

The CLS requires that the array must be a 0 cardinality group.

Each array is associated with some additional information, called overhead information (overhead).

Jiter only checks the array bounds once before performing a loop, not every loop.

Type conversions for 13.1 arrays

For an array of reference types, two array types must have the same dimensions, and from the source element type to the target element type, there must be a type conversion implicitly (to the parent class) or display (to the subclass).

For an array of value types, you cannot convert it to any other type--use the Array.copy method instead:

int32[] Ildim = new Int32[5];

object[] Obldim = new Object[ildim.            Length];

Array.copy (Ildim, Obldim, Ildim. Length);

Array.copy can also make the necessary type conversions when you copy each array element:

* Boxing the elements of a value type into elements of a reference type

* Unpacking the elements of a reference type as an element of a value type

* Widen CLR primitive value types, Int32 to double

* If the type of the two array is incompatible (from object[] to iformattable[]), the element is converted to a downward type.

Covariance of arrays: converting an array from one type to another--there is a performance penalty, and the following code will error at run time:

string[] sa = new string[100];
object[] oa = sa;
OA[5] = "Jax";
OA[3] = 1; Run-time error

If you just need to copy some elements from an array to another array, you can use the System.buffer blockcopy () method to perform faster than array.copy, but only primitive types are supported, not transition capable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.