在Java中,arrays .equals()允许轻松地比较两个基本数组的内容(重载可用于所有基本类型)。
c#中有这样的东西吗?在c#中是否存在比较两个数组内容的“神奇”方法?
在Java中,arrays .equals()允许轻松地比较两个基本数组的内容(重载可用于所有基本类型)。
c#中有这样的东西吗?在c#中是否存在比较两个数组内容的“神奇”方法?
当前回答
这个LINQ解决方案是有效的,不知道它在性能上如何与SequenceEquals相比。但是它处理不同的数组长度,. all将在不遍历整个数组的情况下,在第一个不相等的项上退出。
private static bool arraysEqual<T>(IList<T> arr1, IList<T> arr2)
=>
ReferenceEquals(arr1, arr2) || (
arr1 != null && arr2 != null &&
arr1.Count == arr2.Count &&
arr1.Select((a, i) => arr2[i].Equals(a)).All(i => i)
);
其他回答
我想确定两个集合是否有相同的内容,以任何顺序。这意味着,对于集合A中的每个元素,在两个集合中都有相同数量的具有该值的元素。我想要解释重复(所以{1,2,2,3}和{1,2,3,3}不应该被认为是“相同的”)。
这是我想出的(注意,IsNullOrEmpty是另一个静态扩展方法,如果枚举对象为空或有0个元素,则返回true):
public static bool HasSameContentsAs<T>(this IEnumerable<T> source, IEnumerable<T> target)
where T : IComparable
{
//If our source is null or empty, then it's just a matter of whether or not the target is too
if (source.IsNullOrEmpty())
return target.IsNullOrEmpty();
//Otherwise, if the target is null/emtpy, they can't be equal
if (target.IsNullOrEmpty())
return false;
//Neither is null or empty, so we'll compare contents. To account for multiples of
//a given value (ex. 1,2,2,3 and 1,1,2,3 are not equal) we'll group the first set
foreach (var group in source.GroupBy(s => s))
{
//If there are a different number of elements in the target set, they don't match
if (target.Count(t => t.Equals(group.Key)) != group.Count())
return false;
}
//If we got this far, they have the same contents
return true;
}
你可以使用Enumerable.SequenceEqual。这适用于任何IEnumerable<T>,而不仅仅是数组。
同样对于数组(和元组),你可以使用。net 4.0的新接口:IStructuralComparable和IStructuralEquatable。使用它们不仅可以检查数组是否相等,还可以比较它们。
static class StructuralExtensions
{
public static bool StructuralEquals<T>(this T a, T b)
where T : IStructuralEquatable
{
return a.Equals(b, StructuralComparisons.StructuralEqualityComparer);
}
public static int StructuralCompare<T>(this T a, T b)
where T : IStructuralComparable
{
return a.CompareTo(b, StructuralComparisons.StructuralComparer);
}
}
{
var a = new[] { 1, 2, 3 };
var b = new[] { 1, 2, 3 };
Console.WriteLine(a.Equals(b)); // False
Console.WriteLine(a.StructuralEquals(b)); // True
}
{
var a = new[] { 1, 3, 3 };
var b = new[] { 1, 2, 3 };
Console.WriteLine(a.StructuralCompare(b)); // 1
}
检查这个线程的答案,它将其中一个数组转换为HashSet,并使用SetEquals与另一个数组进行比较。 但是请注意,这并不检查顺序或副本。
可列举的使用。LINQ中的SequenceEqual。
int[] arr1 = new int[] { 1,2,3};
int[] arr2 = new int[] { 3,2,1 };
Console.WriteLine(arr1.SequenceEqual(arr2)); // false
Console.WriteLine(arr1.Reverse().SequenceEqual(arr2)); // true