在 C# 中测试数组的相等性 [英] Testing equality of arrays in C#

查看:27
本文介绍了在 C# 中测试数组的相等性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组.例如:

int[] Array1 = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
int[] Array2 = new[] {9, 1, 4, 5, 2, 3, 6, 7, 8};

确定它们是否具有相同元素的最佳方法是什么?

What is the best way to determine if they have the same elements?

推荐答案

通过使用 LINQ 你可以表达地和高性能地实现它:

By using LINQ you can implement it expressively and performant:

var q = from a in ar1
        join b in ar2 on a equals b
        select a;

bool equals = ar1.Length == ar2.Length && q.Count() == ar1.Length;

这篇关于在 C# 中测试数组的相等性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆