开发者

C# - Compiler Error - while assigning int[] to object[]

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            object[] obj = new object[3];
            obj[0] = new object();
            obj[1] = "some string";
            obj[2] = 10;

            string[] strings = new string[] { "one", "two", "three" };
            obj = strings; //---> No Error here, Why ?

            int[] ints = new int[] { 1, 2, 3 };
            obj = ints; /*-> Compiler error - Cannot implicitly convert type 'int[]' to 'object[]', 开发者_如何学JAVAWhy ?*/ 
        }
    }
}

I get a compiler error while doing the step as shown above. But, in the preceding step, there is no error. Can somebody explain me this behavior ? I am using VS 2010.

EDIT - For sake of completeness, again, this won't compile - Variance support in .NET 4.0 has been cleaned up now. One can use new keywords in and out with generic type parameters.

    List<object> objectList = new List<object>();
    List<string> stringList = new List<string>();
    objectList = stringList;


Only arrays of reference types (like String) may be assignable to arrays of other reference types (like Object). Since int is a value type, arrays of it may not be assigned to arrays of other types.

To be more specific, this is called array covariance. It only works when the bit patterns stored in the array are compatible with the destination type. For example, the bits in a String[] are all references to strings and can be safely copied into memory locations storing references to objects. An array of value types, however, stores the actual data of the elements (as opposed to just references to them). This means that an int[] stores the actual 32-bit integers in the elements of the array. Since a 32-bit integer cannot be safely copied into a memory location storing a reference to an object or any other type, you cannot assign an array of them to an array of any other type.

Note that technically the bits of an int can be safely copied into a memory location storing a uint (and vice-versa). This means that you should be able to do something like int[] x = new uint[10]. This is not actually covariance and C# does not allow it. However, it is legal in the CLR and you can convince C# to let you do it if you want to.


string and object are both reference types. That is, a variable of those types actually stores a pointer to somewhere else in memory. int is a value type. That is, the data is stored directly where the variable is declared.

This means that an array of reference types is fundamentally different to an array of value types. An array of reference types is stored as an array of pointers. Because pointers are all the same size, this means that to re-interpret a string[] as an object[] simpy means adjusting the type checking done when accessing the items in the array (roughly speaking).

However, in an int[], the values are stored in the array directly; the int values are concatenated together. No pointers are involved. This means that to re-interpret an int[] as an object[] requires boxing every value stored in the array into an object. Hence why you can't do it using a simple cast or assignment - it's an O(n) operation that creates a new array. Instead, you can use Array.Copy which deals with all the boxing for you.


Why that is not working is stated in many answers here, so I will not try to copy-paste what they said.

If you, or someone else really want to do that (convert int[] to object[]) for some reason, you can use LINQ like so:

int[] ints = new int[] { 1, 2, 3 };
object[] obj_ints = (from i in ints select i).Cast<object>().ToArray();

;)


what I see is an array of reference types can be assigned of another array of reference types while an array of value types cannot. Make sense to me


If you have a Visual Studio installation you will find the C# Language Speicifcation as doc file somewhere under VC# . Chapter 12.5 deals with covariance and there it says

For any two reference-types A and B, if an implicit reference conversion (§6.1.6) or explicit reference conversion (§6.2.4) 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). This relationship is known as array covariance.

This may not answer your question, but it was a deliberate decision in the spec to do it that way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜