By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,130 Members | 2,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,130 IT Pros & Developers. It's quick & easy.

Generic List Equals Method Failing

P: 2
I'm trying to use the Equals() method that is defined on the List(Of T) class. The docs seem to state that if T implements the IEquatable interface, then it will use the Equals method that is declared there in order to do an Equals() call on the List itself.

So I create a class called ExportStream which implements IEquatable (and the Object.Equals method as well). Next when I put the instances of ExportStream into lists and compare, the Lists tell me that they are not equal when they should be equal.

I've also tried setting break points inside the Equals() method of the ExportStream and it is not hitting those breakpoints.

Anyone see something I am doing wrong that is keeping this from working? Or do we know for sure that this feature of the List<T> class is broken?

Expand|Select|Wrap|Line Numbers
  1.         static void Main(string[] args)
  2.         {
  3.             ExportStream one = new ExportStream("one", new byte[] { 1, 2, 3, 4, 5 });
  4.             ExportStream two = new ExportStream("two", new byte[] { 6, 7, 8, 9, 10 });
  5.             ExportStream three = new ExportStream("three", new byte[] { 1, 2, 3, 4, 5 });
  6.  
  7.             List<ExportStream> a = new List<ExportStream>(new ExportStream[] { one, two });
  8.             List<ExportStream> b = new List<ExportStream>(new ExportStream[] { three });
  9.             List<ExportStream> c = new List<ExportStream>(new ExportStream[] { one, two });
  10.  
  11.             Console.WriteLine("a.Equals(b) = {0}", a.Equals(b).ToString());
  12.             Console.WriteLine("a.Equals(c) = {0}", a.Equals(c).ToString());
  13.             Console.ReadLine();
  14.         }
  15.  
  16.         public class ExportStream : IEquatable<ExportStream>
  17.         {
  18.             public string FileName;
  19.             public byte[] ImageData;
  20.  
  21.             public ExportStream(string fileName, byte[] imageData)
  22.             {
  23.                 this.FileName = fileName;
  24.                 this.ImageData = imageData;
  25.             }
  26.  
  27.             public bool Equals(ExportStream other)
  28.             {
  29.                 if (other == null)
  30.                 {
  31.                     throw new ArgumentException("other");
  32.                 }
  33.                 else
  34.                 {
  35.                     byte[] myData = this.ImageData;
  36.                     byte[] otherData = other.ImageData;
  37.  
  38.                     if (!myData.Length.Equals(otherData.Length))
  39.                     {
  40.                         return false;
  41.                     }
  42.                     else
  43.                     {
  44.                         for (int i = 0; i < myData.Length; i++)
  45.                         {
  46.                             if (!myData[i].Equals(otherData[i]))
  47.                             {
  48.                                 return false;
  49.                             }
  50.                         }
  51.  
  52.                         return true;
  53.                     }
  54.                 }
  55.             }
  56.         }
  57.  
Oct 17 '06 #1
Share this Question
Share on Google+
1 Reply


P: 2
I read the docs incorrectly. I am now realizing that the fact that List<T> has an Equals() method is only because it inherits from System.Object. The docs actually state that the Compare(), Contains(), etc use the Equals() method.
Oct 17 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.