C#中:你如何检查表是相同的尺寸和放大器;相同的元素?放大器、元素、尺寸、检查表

由网友(﹌ 只是一个滥情的年代)分享简介:有串的两个列表List A;List B;什么是最短code,你会建议检查A.Count == B.Count和A在B和反之亦然每一个元素:每一个B是A(A项和B项可能有不同订购)。 What is the shortest code you would suggest to ch...

有串的两个列表

List<string> A;
List<string> B;

什么是最短code,你会建议检查A.Count == B.Count和A在B和反之亦然每一个元素:每一个B是A(A项和B项可能有不同订购)。

What is the shortest code you would suggest to check that A.Count == B.Count and each element of A in B and vise versa: every B is in A (A items and B items may have different order).

推荐答案

如果你不需要担心重复:

If you don't need to worry about duplicates:

bool equal = new HashSet<string>(A).SetEquals(B);

如果您担心重复,即变得稍微别扭。这将工作,但它的速度相对较慢:

If you are concerned about duplicates, that becomes slightly more awkward. This will work, but it's relatively slow:

bool equal = A.OrderBy(x => x).SequenceEquals(B.OrderBy(x => x));

当然,你可以先检查次数,这是一个简单的EX pression这两个选项更有效。例如:

Of course you can make both options more efficient by checking the count first, which is a simple expression. For example:

bool equal = (A.Count == B.Count) && new HashSet<string>(A).SetEquals(B);

...但你问最短code:)

... but you asked for the shortest code :)

阅读全文

相关推荐

最新文章