Jon Jagger
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 19 Arraysprevious at this levelnext at this level 19.5 Array covariance Paragraph 11 For any two reference-types A and B, if an implicit reference conversion (§13.1.4) or explicit reference conversion (§13.2.3) exists from A to B, then the same reference conversion also exists from the array type A[R] to the array type B[R], where R is any given rank-specifier (but the same for both array types). 2 This relationship is known as array covariance. 3 Array covariance, in particular, means that a value of an array type A[R] may actually be a reference to an instance of an array type B[R], provided an implicit reference conversion exists from B to A. Paragraph 21 Because of array covariance, assignments to elements of reference type arrays include a run-time check which ensures that the value being assigned to the array element is actually of a permitted type (§14.13.1). [Example: For example:
class Test  
   static void Fill(object[] array, int index, int count, object value) {  
      for (int i = index; i < index + count; i++) array[i] = value;  
   static void Main() {  
      string[] strings = new string[100];  
      Fill(strings, 0, 100, "Undefined");  
      Fill(strings, 0, 10, null);  
      Fill(strings, 90, 10, 0);  
The assignment to array[i] in the Fill method implicitly includes a run-time check, which ensures that the object referenced by value is either null or an instance of a type that is compatible with the actual element type of array. In Main, the first two invocations of Fill succeed, but the third invocation causes a System.ArrayTypeMismatchException to be thrown upon executing the first assignment to array[i]. The exception occurs because a boxed int cannot be stored in a string array. end example] Paragraph 31 Array covariance specifically does not extend to arrays of value-types. 2 For example, no conversion exists that permits an int[] to be treated as an object[].
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS