Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why C# behaves differently on two int array syntaxes

Tags:

arrays

c#

Array in C# is co-variant implicitly on reference type:

object[] listString = new string[] { "string1", "string2" }; 

But not on value type, so if you change string to int, you will get compiled error:

object[] listInt = new int[] {0, 1}; // compile error 

Now, the concern is when you declare int array like two syntaxes below which do not explicitly declare the type int, just only differentiate on new[], compiler will treat differently:

object[] list1 = { 0, 1 };       //compile successfully object[] list2 = new[] {0, 1};    //compile error 

You will get object[] list1 = { 0, 1 }; compiled successfully, but object[] list2= new[] {0, 1}; compiled error.

It seems the C# compiler treats

object[] list1 = { 0, 1 }; 

as

object[] list1 = new object[]{ 0, 1 }; 

but

object[] list2 = new[] { 0, 1 }; 

as

object[] list2 = new int[]{ 0, 1 };  //error because of co-variant 

Why C# compiler behaves in the different way on this case?

like image 737
cuongle Avatar asked May 09 '13 08:05

cuongle


People also ask

Why is C used?

C is a general-purpose programming language and can efficiently work on enterprise applications, games, graphics, and applications requiring calculations, etc. C language has a rich library which provides a number of built-in functions. It also offers dynamic memory allocation.

Why is C used in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

Why should you learn C?

C is very fast in terms of execution time. Programs written and compiled in C execute much faster than compared to any other programming language. C programming language is very fast in terms of execution as it does not have any additional processing overheads such as garbage collection or preventing memory leaks etc.

Why is C language named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C". C is about the tone C.


1 Answers

The version that compiles uses an array initializer to initialize list1. The C# language spec, §1.110 ("Array initializers") states:

An array initializer consists of a sequence of variable initializers, enclosed by “{”and “}” tokens and separated by “,” tokens. Each variable initializer is an expression or, in the case of a multi-dimensional array, a nested array initializer.

The context in which an array initializer is used determines the type of the array being initialized. In an array creation expression, the array type immediately precedes the initializer, or is inferred from the expressions in the array initializer. In a field or variable declaration, the array type is the type of the field or variable being declared.

When an array initializer is used in a field or variable declaration, such as:

int[] a = {0, 2, 4, 6, 8}; 

it is simply shorthand for an equivalent array creation expression:

int[] a = new int[] {0, 2, 4, 6, 8}; 

So it is obvious that this should compile.

The second version uses an explicit array creation expression, where you instruct the compiler specifically what type of array to create. §1.51.10.4 ("Array creation expressions") states:

An array creation expression of the third form is referred to as an implicitly typed array creation expression. It is similar to the second form, except that the element type of the array is not explicitly given, but determined as the best common type (§1.50.2.14) of the set of expressions in the array initializer.

Therefore, the second version is equivalent to

object[] list2 = new int[] { 0, 1 }; 

So the question now effectively becomes "why can I not assign an int[] to an object[]", just as you mention at the end of the question. And the answer is also simple, given in §1.109 ("Array covariance"):

Array covariance specifically does not extend to arrays of value-types. For example, no conversion exists that permits an int[] to be treated as an object[].

like image 176
Jon Avatar answered Sep 25 '22 08:09

Jon