阵列测试平等在C# [英] Testing equality of arrays in C#

查看:84
本文介绍了阵列测试平等在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 可以实现它的前pressively和高性能:

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天全站免登陆