Why SequenceEqual for List returns false?

前端 未结 3 1542
忘了有多久
忘了有多久 2020-12-18 00:29

Hi I have some problems with sequenceEqual when I have situation like this:

Sentence s1 = new Sentence { Text = \"Hi\", Order = 1 };
Sentence s2 = new Senten         


        
3条回答
  •  难免孤独
    2020-12-18 01:08

    You are creating two new instances of Sentence every time you call Getall(). When comparing the elements in the list, SequenceEqual will use the default equality comparer, which basically just check that they refer the sme object: they don't, so they are different.

    What you can do is override the Equal() and == operators in Sequence to have equality check other properties (like Text and Order).

    Alternatively you can write an IEqualityComparer and pass it to SequenceEqual

提交回复
热议问题