Bạn có thể viết một phương pháp mở rộng như vậy:
public static class ListExtensions
{
public static bool IsEqual<T>(this IList<T> list,IList<T> target, IComparer<T> comparer) where T:IComparable<T>
{
if (list.Count != target.Count)
{
return false;
}
int index = 0;
while (index < list.Count &&
comparer.Compare(list[index],target[index]) == 0)
{
index++;
}
if (index != list.Count)
{
return false;
}
return true;
}
}
Và gọi nó như vậy:
List<int> intList = new List<int> { 1, 234, 2, 324, 324, 2 };
List<int> targetList = new List<int> { 1, 234, 2, 324, 324 };
bool isEqual = intList.IsEqual(targetList, Comparer<int>.Default);
EDIT: Cập nhật mã để sử dụng một phương pháp tĩnh thay vì kể từ khi OP đang sử dụng .NET 3,0
public static bool IsEqual<T>(IList<T> sourceList, IList<T> targetList, IComparer<T> comparer) where T : IComparable<T>
{
if (sourceList.Count != targetList.Count)
{
return false;
}
int index = 0;
while (index < sourceList.Count &&
comparer.Compare(sourceList[index], targetList[index]) == 0)
{
index++;
}
if (index != sourceList.Count)
{
return false;
}
return true;
}
Chủ đầu tư:
bool isEqual = IsEqual(intList,targetList, Comparer<int>.Default);
Nguồn
2009-10-10 04:12:27
thể trùng lặp của [Thế nào là cách tốt nhất để kiểm tra hai Danh sách danh sách cho sự bình đẳng trong C#] (http://stackoverflow.com/questions/876508/what-is-the-best-way-to-check-hai-listt-danh sách-cho-bình đẳng-in-c-sắc nét) –
nawfal