我试图将两个数组相互比较.我试过这段代码并得到以下错误.
static bool ArraysEqual(Array a1, Array a2)
{
if (a1 == a2)
return true;
if (a1 == null || a2 == null)
return false;
if (a1.Length != a2.Length)
return false;
IList list1 = a1, list2 = a2; //error CS0305: Using the generic type 'System.Collections.Generic.IList<T>' requires '1' type arguments
for (int i = 0; i < a1.Length; i++)
{
if (!Object.Equals(list1[i], list2[i])) //error CS0021: Cannot apply indexing with [] to an expression of type 'IList'(x2)
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
为什么我会收到这个错误?我选择了一种低技术解决方案并且做得很好,但我需要为每种类型复制/粘贴几次.
static bool ArraysEqual(byte[] a1, byte[] a2)
{
if (a1 == a2)
return true;
if (a1 == null || a2 == null)
return false;
if (a1.Length != a2.Length)
return false;
for (int i = 0; i < a1.Length; i++)
{
if (a1[i] != a2[i])
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
Nol*_*rin 159
假设你有LINQ可用而且不太关心性能,最简单的事情如下:
var arraysAreEqual = Enumerable.SequenceEqual(a1, a2);
Run Code Online (Sandbox Code Playgroud)
实际上,可能需要使用Reflector或ILSpy来检查SequenceEqual方法实际上做了什么,因为它可以很好地优化数组值的特殊情况!
Mar*_*ell 73
"为什么我会收到这个错误?" - 可能,你没有" using System.Collections;"在文件的顶部 - 只有" using System.Collections.Generic;" - 但是,泛型可能更安全 - 见下文:
static bool ArraysEqual<T>(T[] a1, T[] a2)
{
if (ReferenceEquals(a1,a2))
return true;
if (a1 == null || a2 == null)
return false;
if (a1.Length != a2.Length)
return false;
EqualityComparer<T> comparer = EqualityComparer<T>.Default;
for (int i = 0; i < a1.Length; i++)
{
if (!comparer.Equals(a1[i], a2[i])) return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
Yul*_*mok 13
对于.NET 4.0及更高版本,您可以使用StructuralComparisons类型比较数组或元组中的元素:
object[] a1 = { "string", 123, true };
object[] a2 = { "string", 123, true };
Console.WriteLine (a1 == a2); // False (because arrays is reference types)
Console.WriteLine (a1.Equals (a2)); // False (because arrays is reference types)
IStructuralEquatable se1 = a1;
//Next returns True
Console.WriteLine (se1.Equals (a2, StructuralComparisons.StructuralEqualityComparer));
Run Code Online (Sandbox Code Playgroud)
推荐SequenceEqual是好的,但是认为它可能比平常更快(;;)循环太天真了.
这是反映的代码:
public static bool SequenceEqual<TSource>(this IEnumerable<TSource> first,
IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
{
if (comparer == null)
{
comparer = EqualityComparer<TSource>.Default;
}
if (first == null)
{
throw Error.ArgumentNull("first");
}
if (second == null)
{
throw Error.ArgumentNull("second");
}
using (IEnumerator<TSource> enumerator = first.GetEnumerator())
using (IEnumerator<TSource> enumerator2 = second.GetEnumerator())
{
while (enumerator.MoveNext())
{
if (!enumerator2.MoveNext() || !comparer.Equals(enumerator.Current, enumerator2.Current))
{
return false;
}
}
if (enumerator2.MoveNext())
{
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它使用了2个枚举器并触发了许多方法调用,这些调用严重降低了所有内容.它也根本不检查长度,所以在不好的情况下,它可能会慢得多.
比较移动两个迭代器与美丽
if (a1[i] != a2[i])
Run Code Online (Sandbox Code Playgroud)
你会知道我对表演的意思.
它可用于性能确实不那么重要的情况,可能用于单元测试代码,或者在很少调用的方法中使用一些短列表的情况.
| 归档时间: |
|
| 查看次数: |
113675 次 |
| 最近记录: |