|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
comparing two generic Listshi,
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. 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 > > "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 default implementation fo the equality operator is to use 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 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 >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. 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. |
|||||||||||||||||||||||