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[]', Why ?*/
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With