如何比较两个数组 [英] how to compare two arrays
本文介绍了如何比较两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
array1 =firstname,lastname,company
array2相同
但
如果我使用
bool valid = array1.Equals(array2);
它总是返回false.why?
解决方案
因为数组始终是引用类型:这意味着Equals比较两个引用,而不是数组的内容。
相反,尝试 Enumerable.SequenceEqual [ ^ ]将比较两个数组内容。
检查出来:比较2个字符串数组不能正常工作 [ ^ ]
如何比较两个数组 - http://mantascode.com/c-how-to-compare-two-arrays / [ ^ ]
array1="firstname","lastname","company"
array2 is same
but
if I use
bool valid = array1.Equals(array2);
it always return false.why?
解决方案
Because an array is always a reference type: which means that Equals compares the two references, not the contents of the array.
Instead, try Enumerable.SequenceEqual[^] which will compare the two array contents.
Check this out: Comparing 2 string arrays not working properly[^]
How to compare two arrays - http://mantascode.com/c-how-to-compare-two-arrays/[^].
这篇关于如何比较两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文