comparing two generic Lists

I

Irfan

hi,

In the simplest form, i have two generic lists and i want to compare if the
elements in each of them exactly match.

list<double> list1 = new list<double>();
list1.AddRange(new double[]{1,2});

list<double> list2 = new list<double>();
list2.AddRange(new double[]{1,2});

list1 == list2 returns false

Can someone point me to the right direction.

TIA
Irfan
 
N

Nicole Calinoiu

The equality operator in this case is comparing the references to the two
lists, not their contents. If you wish to compare the contents, you'll need
to write your own code to do so.
 
J

Joanna Carter [TeamB]

"Irfan" <[email protected]> a écrit dans le message de (e-mail address removed)...

| In the simplest form, i have two generic lists and i want to compare if
the
| elements in each of them exactly match.
|
| list<double> list1 = new list<double>();
| list1.AddRange(new double[]{1,2});
|
| list<double> list2 = new list<double>();
| list2.AddRange(new double[]{1,2});
|
| list1 == list2 returns false

The default implementation fo the equality operator is to use
ReferenceEquals, which simply compares the references to the objects, not
their contents.

You will have to iterate over both lists, comparing elements one by one.

Joanna
 
M

Mattias Sjögren

Can someone point me to the right direction.

Compare element by element.


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top