comparing two generic Lists

  • Thread starter Thread starter Irfan
  • Start date Start date
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
 
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.
 
"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
 
Can someone point me to the right direction.

Compare element by element.


Mattias
 
Back
Top