<分区>
Possible Duplicate:
Comparing two collections for equality
我需要验证两个 IEnumerable<T>
列表具有相同的元素,不一定以相同的顺序。
我的目标是 .NET 3.5。
这是测试。问题是,应该如何HasSameElements()
实现?
var l1 = new[]{1,2,3};
var l2 = new[]{3,1,2};
bool rez1 = l1.HasSameElements(l2);//should be true
var l3 = new[]{1,2,3,2};
var l4 = new[]{3,1,2,2};
bool rez2 = l3.HasSameElements(l4);//should be true
var l5 = new[]{1,2,3,2};
var l6 = new[]{1,2,3};
bool rez3 = l5.HasSameElements(l6);//should be false
附加说明:
在示例中,我使用的是 IEnumerable,但 T 可以是任何东西。 T 是否必须实现
IComparable
?Enumerable.SequenceEquals() 本身不起作用,它希望元素具有相同的顺序。
这是
HasElements
的模板:
[只是一些占位符文本作为 Markdown“代码格式”错误的解决方法]
public static class Extensions {
public static bool HasElements(this IEnumerable<T> l1, IEnumerable<T> l2){
throw new NotImplementedException();
}
}