collection doesn't find element

G

Guest

Hello,

I have this class:

public class TestClass
{
private string testData = "";
public string TestData
{
get { return this.testData; }
set { this.testData = value; }
}

//public override bool Equals(object obj)
//{
// return this.testData.Equals(obj);
//}

//public override int GetHashCode()
//{
// return this.testData.GetHashCode();
//}
}

and I use it like this:

static void Main(string[] args)
{
TestClass test = new TestClass();
ArrayList ary = new ArrayList();

test.TestData = "test data";

ary.Add(test);

Trace.WriteLine(((TestClass)ary[0]).TestData);
Debug.Assert(ary.Contains(test), "Asserting the collection
contains <test>.");
}

With the *Equals* override commented everything works fine: the trace writes
the "test data" string and the assert is quiet.

Could somebody please tell me why if only the *Equals* override is
uncommented the trace still works fine but the assert fails ?

Note: commenting/uncommenting the GetHashCode() doesn't seem to make any
difference.

Thanks in advance...
 
G

Guest

Well, peer review seems to be indeed a good thing: I didn't notice equals
compares a String with a TestClass !...
 

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