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 ?*/
}
}
}
Recibo un error de compilación mientras hago el paso como se muestra arriba. Pero, en el paso anterior, no hay error. ¿Alguien puede explicarme este comportamiento? Estoy usando VS 2010.C# - Error del compilador - al asignar int [] al objeto []
EDITAR - Para completar, una vez más, esto no se compilará - El soporte de varianza en .NET 4.0 se ha limpiado ahora. Se pueden usar palabras clave nuevas en y en con parámetros de tipo genérico.
List<object> objectList = new List<object>();
List<string> stringList = new List<string>();
objectList = stringList;
¿Usted entiende las diferencias entre tipo de valor y tipos de referencia? – Oded