簡體   English   中英

比較兩個相同的字符串列表

[英]compare two identical lists of strings

假設我有以下代碼:

    List<string> numbers = new List<string> { "1", "2" };
    List<string> numbers2 = new List<string> { "1", "2"};

    if (numbers.Equals(numbers2))
    {

    }

就像您看到的一樣,我有兩個包含相同項目的列表。 有沒有一種方法可以通過使用一種方法檢查這兩個列表是否相等?

解:

使用SequenceEqual()

謝謝

使用Enumerable.SequenceEqual ,但首先對列表進行Sort

// if order does not matter
bool theSame = numbers.Except(numbers2).Count() == 0;

// if order is matter
var set = new HashSet<string>(numbers);
set.SymmetricExceptWith(numbers2);
bool theSame = set.Count == 0;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM