Comparing two List for equality

后端 未结 9 1390
时光说笑
时光说笑 2020-12-01 11:30

Other than stepping through the elements one by one, how do I compare two lists of strings for equality (in .NET 3.0):

This fails:

// Expected re         


        
9条回答
  •  感动是毒
    2020-12-01 12:22

    Using Linq and writing the code as an extension method :

    public static bool EqualsOtherList(this List thisList, List theOtherList)
    {
      if (thisList == null || theOtherList == null || 
          thisList.Count != theOtherList.Count) return false;
      return !thisList.Where((t, i) => !t.Equals(theOtherList[i])).Any();
    }
    

提交回复
热议问题