Home All Groups Group Topic Archive Search About

comparing two generic Lists

Author
2 Jan 2007 6:15 PM
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

Author
2 Jan 2007 7:33 PM
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.



Show quote
"Irfan" <ir***@asc-ltd.co.uk> wrote in message
news:OE9iCopLHHA.1008@TK2MSFTNGP06.phx.gbl...
> 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
>
>
Author
2 Jan 2007 7:34 PM
Joanna Carter [TeamB]
"Irfan" <ir***@asc-ltd.co.uk> a écrit dans le message de news:
OE9iCopLHHA.1***@TK2MSFTNGP06.phx.gbl...

| 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

--
Joanna Carter [TeamB]
Consultant Software Engineer
Author
2 Jan 2007 7:52 PM
Mattias Sjögren
>Can someone point me to the right direction.

Compare element by element.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
5 Jan 2007 3:14 PM
Irfan
thanks for all the replies,

irfan

Show quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:OwgkaeqLHHA.140@TK2MSFTNGP04.phx.gbl...
> >Can someone point me to the right direction.
>
> Compare element by element.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.

AddThis Social Bookmark Button