C# - 编译器错误 - 分配INT []反对[] [英] C# - Compiler Error - while assigning int[] to object[]

查看:112
本文介绍了C# - 编译器错误 - 分配INT []反对[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 命名空间的ConsoleApplication1 
{
类节目
{
静态无效的主要(字串[] args)
{
对象[] = OBJ新的对象[3];
物镜[0] =新的对象();
OBJ [1] =一些字符串;
物镜[2] = 10;

的String [] =字符串新的字符串[] {一,二,三化};
OBJ =串; // --->这里没有错误,为什么呢?

INT []整数=新INT [] {1,2,3};
OBJ =整数; / * - >编译器错误 - 无法隐式转换类型'诠释[]'到'对象[]',为什么* /
}
}
}
<? / pre>

我得到一个编译错误,而这样做的步骤,如上图所示。但是,在前面的工序中,不存在误差。有人可以解释我这种行为?我使用VS 2010




编辑 - 为了完整起见,又来了,这不会编译 - 在.NET 4.0中支持方差有现在已经被清理。我们可以使用新的关键字的和的的泛型类型参数。




 列表<对象>链表类=新的List<对象>(); 
名单,LT;字符串> StringList的=新的List<串GT;();
链表类= StringList的;


解决方案

仅供参考类型的数组(如字符串)可分配给其他引用类型的数组(如对象)。由于 INT 是值类型,它的阵列,不得分配给其他类型的数组。



要成为更具体的,这就是所谓的阵列协方差 。它仅当存储阵列中的位模式与目标类型兼容的工作原理。例如,在一位串[] 对字符串的所有引用,并且可以被安全地拷贝到存储对对象的引用的存储器位置。值类型的阵列,然而,存储元件的实际数据(而不是只对它们的引用)。这意味着一个 INT [] 存储实际的32位整数数组的元素。因为一个32位整数不能安全地复制到存储器位置存储到对象或任何其它类型的引用,不能将它们的阵列分配给任何其它类型的阵列。



注意的技术上位 INT 可以安全地复制到一个存储单元存储 UINT (反之亦然)。这意味着你应该能够做到像 INT [] X =新的uint [10] 。这不是真正的协方差和C#不允许它。然而,它在CLR法律和你能说服C#让,如果你想给你做。


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.

这篇关于C# - 编译器错误 - 分配INT []反对[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆