Why SequenceEqual for List returns false?

前端 未结 3 1536
忘了有多久
忘了有多久 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:13

    Your problem is that one new Sentence { Text = "Hi", Order = 1 } is not equal to another new Sentence { Text = "Hi", Order = 1 }. Although the contents are the same, you have two separate objects, and unless you've designed your class otherwise they are not equal to each other unless they are literally the same objects (as in your first example).

    Your Sentence class needs to override Equals and GetHashCode, at the very least, at which point your SequenceEquals should work again.

提交回复
热议问题