我想要做到这一点:
List<Parent> test = new List<Child>();
我的类的完整代码如下:
class Program
{
public static void Main(string[] args)
{
List<Parent> test = new List<Child>();
test.Add(new Child());
test.Add(new AnotherChild());
}
}
class Parent { }
class Child : Parent { }
class AnotherChild : Parent { }
请问有人能解释一下为什么会出现这个错误:
错误 2 无法将类型“System.Collections.Generic.List”转换为类型“System.Collections.Generic.List” d:\personal\documents\visual studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs 20 24 ConsoleApplication3
而这段代码为什么可以正常工作?
Parent[] test = new Child[10];
List<Parent> result = test.ToList();
谢谢 :)
-- 正确的翻译:
现在我知道为什么:List被编译成List`1
,而List则被编译成List`2
。它们之间没有关系。