Tuesday, 15 April 2014

c# - Why SequenceEqual for List returns false? -



c# - Why SequenceEqual for List<T> returns false? -

hi have problems sequenceequal when have situation this:

sentence s1 = new sentence { text = "hi", order = 1 }; sentence s2 = new sentence { text = "hello", order = 2 }; list<sentence> list1 = new list<sentence> { s1, s2 }; list<sentence> list2 = new list<sentence> { s1, s2 };

and works fine

bool equal = list1.sequenceequal(list2);

and returns true;

but when have method returns list<sentence> example:

public list<sentence> getall() { sentence s1 = new sentence { text = "hi", order = 1 }; sentence s2 = new sentence { text = "hello", order = 2 }; homecoming new list<sentence> { s1, s2 }; }

and utilize this:

list<sentence> list1 = getall(); list<sentence> list2 = getall();

and simply, check it

bool equal = list1.sequenceequal(list2);

it returns 'false', please tell me why? , how create work?

your problem 1 new sentence { text = "hi", order = 1 } not equal new sentence { text = "hi", order = 1 }. although contents same, have 2 separate objects, , unless you've designed class otherwise not equal each other unless literally same objects (as in first example).

your sentence class needs override equals , gethashcode, @ least, @ point sequenceequals should work again.

c# list comparison

No comments:

Post a Comment