简体   繁体   中英

compare two identical lists of strings

Let's say I have following code:

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

    if (numbers.Equals(numbers2))
    {

    }

Like you can see I have two lists with identical items. Is there a way to check if these two lists are equal by using one method?

SOLUTION:

Use SequenceEqual()

Thanks

使用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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM