Comparing two List for equality

后端 未结 9 1392
时光说笑
时光说笑 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条回答
  •  猫巷女王i
    2020-12-01 12:04

    It might be used with not regular way but without implementation IEquatable for Custom types

    JsonConvert.SerializeObject( myList1) == JsonConvert.SerializeObject( myList2)
    

    But in general case you could use SequenceEqual as was mentioned in comments https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?view=netframework-4.8

    Also do not forget to implement IEquatable interface for custom types (not necessary for strings type or other structure)

提交回复
热议问题