• 0

[C#] Classes and Lists


Question

Hello, I'm getting something weird happening in my program that I don't understand. I've got the following code

			Obj temp = new Obj("test", "test");
			Obj temp2 = new Obj("test", "test");

			Obj2.someList.Add(temp);

			MessageBox.Show(Obj2.someList.Contains(temp));
			MessageBox.Show(Obj2.someList.Contains(temp2));

I expect BOTH message boxes to show true, but only the first one does, whilst the second one returns false? Howcome?

Note: Obj2's someList is a static public List<> hence statically called and not through an instance.

Thanks

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

I guess I wasn't clear aren't temp2 and temp the same object instance? Similar to

int temp = 1;
int temp2 = 1;

Obj2.someList.Add(temp);

MessageBox.Show(Obj2.someList.Contains(temp));
MessageBox.Show(Obj2.someList.Contains(temp2));

Which returns 2 in both cases.

Aren't the objects temp and temp2 the same (in my first post)?

Link to comment
Share on other sites

  • 0

The Contains() method is looking at the actual objects, not their values, unless you write your own custom Contains() method to do so.

Edit: In the int example you posted, int is treated as a pure value, not as an object reference. If you were to use Integer instead (an object wrapper for int), it would act like the code in your first post.

Link to comment
Share on other sites

  • 0

When you create a new object instance both instances are totally independent to each other, meaning if you change one instance the other won't be affected.

Link to comment
Share on other sites

  • 0
I guess I wasn't clear aren't temp2 and temp the same object instance? Similar to

int temp = 1;
int temp2 = 1;

Obj2.someList.Add(temp);

MessageBox.Show(Obj2.someList.Contains(temp));
MessageBox.Show(Obj2.someList.Contains(temp2));

Which returns 2 in both cases.

Aren't the objects temp and temp2 the same (in my first post)?

temp2 and temp are not the same object instance. (in either situations). They are both two separate objects. The reason it probably works with the int's above is because they both have the same value. Try changing the value of temp2 to '2' and see what happens.

To be more clear it seems the Contains function is searching the list for a value equal to that which is passed in the parameter. Using the integer values above it is only searching the list for a value of '1'. Since you have added the value of '1' to the list already it's going to find it.

However, with the Objects used in your original post this will not work. Even though the two objects in your original post have the same values added to them they both return two different object references. So this time when you pass the Object temp2 to the Contains function it is searching for a reference to that specific object. Which of course it can not find as it has not yet been added to the list.

At least that is my understanding of what is going on.

Edited by SOOPRcow
Link to comment
Share on other sites

  • 0

So the best thing is override the contain method, and then check the object members my self?

Kudos on the quick reply guys :D

Link to comment
Share on other sites

  • 0
So the best thing is override the contain method, and then check the object members my self?

Kudos on the quick reply guys :D

If that is for sure the behavior you want it to have, then that would work. Maybe make it a new method with a different name, because someone looking at your code might not realize that you've changed the default behavior of Contains() in that situation.

Link to comment
Share on other sites

  • 0

References are for classes. Int32 is a structure and it's passed by value, so

int temp1 = 1;
int temp2 = 1;

are two separate values.

Form form1 = new Form();
Form form2 = form1;

are classes and passed by reference, so both will both point to the same instance.

Link to comment
Share on other sites

  • 0

List(T).Contains method determines equality using the default equality comparer for T, the type of values in the list. For ints, that would be the value; for Obj (your user-defined type), that would be the instance.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.