[英]c# generic list merge
我无法合并列表和列表? OOP说MyType2是MyType ...
using System;
using System.Collections.Generic;
namespace two_list_merge
{
public class MyType
{
private int _attr1 = 0;
public MyType(int i)
{
Attr1 = i;
}
public int Attr1
{
get { return _attr1; }
set { _attr1 = value; }
}
}
public class MyType2 : MyType
{
private int _attr2 = 0;
public MyType2(int i, int j)
: base(i)
{
Attr2 = j;
}
public int Attr2
{
get { return _attr2; }
set { _attr2 = value; }
}
}
class MainClass
{
public static void Main(string[] args)
{
int count = 5;
List<MyType> list1 = new List<MyType>();
for(int i = 0; i < count; i++)
{
list1[i] = new MyType(i);
}
List<MyType2> list2 = new List<MyType2>();
for(int i = 0; i < count; i++)
{
list1[i] = new MyType2(i, i*2);
}
list1.AddRange((List<MyType>)list2);
}
}
}
我将假设您没有使用C#4.0。
在早期版本的C#,这是行不通的,因为语言不支持泛型类型的逆变和协方差 。
不用担心学术术语-它们只是允许的变异(即变异)种类的术语。
这是一篇关于细节的好文章: http : //blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx
为了使您的代码正常工作,请编写以下代码:
list1.AddRange(list2.Cast<MyType>());
如果您使用的是C#4(.NET 4),则只需在最后一行中删除演员表:
list1.AddRange(list2);
如果使用的是C#3(.NET 3.5),则需要使用Cast()LINQ扩展:
list1.AddRange(list2.Cast<MyType>());
您不能将list2强制转换为List的原因是List不是协变的。 您可以在这里找到为什么不是这种情况的很好的解释:
在C#中,为什么不能将List <string>对象存储在List <object>变量中
第一行起作用的原因是AddRange()采用IEnumerable,而IEnumerable是协变的。 .NET 3.5没有实现通用集合的协方差,因此在C#3中需要Cast()。
如果可以的话,也许尝试使用LINQ以及对MyType
的显式转换。 使用C#4。
List<MyType> list1 = new List<MyType>
{ new MyType(1), new MyType(2), new MyType(3)};
List<MyType2> list2 = new List<MyType2>
{ new MyType2(11,123), new MyType2(22,456), new MyType2(33, 789) };
var combined = list1.Concat(list2.AsEnumerable<MyType>());
您不能这样做,因为MyType2是MyType,但是List<MyType2>
不是List<MyType>
。 2个List<XXX>
类型之间没有继承关系。
您可以使用LINQ的Cast方法轻松实现复制,该方法会将每个元素转换为所需的类型。
list1.AddRange(list2.Cast<MyType>());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.