0

I'm writing a simple contains function which accepts a value and checks if it exists in the linked list. Returns a bool. However, when temp.Data is the same as data, as checked through debugging, it still proceeds to enter the while statement and cycle through the list, as if they are not equal. I have found a solution, by using toString() on all values. So I have to compare temp.Data.ToString() and data.ToString(). Then it works...I am new to C#, I haven't used the Object keyword much. I'm wondering if it's something to do with that? So my function works with my quick fix but I'm trying to understand why...thanks!

public bool Contains(Object data)
    {
        Node temp = head;
        while((temp.Data != data) && (temp.Next != null))
        {
            temp = temp.Next;
        } 
//Rest of code 
Birdman
  • 1,404
  • 5
  • 22
  • 49
  • `Object` is not a specific keyword like you might think it is. In the hierarchy of all Classes it is just the top level class from which all other classes inherit (this will only make sense to you if you have studied object oriented programming yet (which - if otherwise - you should do asap)) – Neuron Nov 05 '17 at 00:10
  • You need to understand the difference between identity and equality in c#. Try calling `Equals(data)`. See the following: https://blogs.msdn.microsoft.com/csharpfaq/2004/03/29/when-should-i-use-and-when-should-i-use-equals/ – astidham2003 Nov 05 '17 at 00:13
  • This code checks if the references are the same: `temp.Data != data`. So if you have two things that have the same fields but they are different instances then it will return true because the references are not the same. Either override the `Equals` method or compare the field to each other instead of the whole object. – CodingYoshi Nov 05 '17 at 00:20
  • (I think that...) Comparing two reference-type objects normally checks if they both refer to the same location in memory. Otherwise, if one of them isn't null, it will call the `Equals` method of that one against the other (which usually compares the property values of the two objects, but is an implementation detail of the class). In the case of calling `ToString` on the objects, you are comparing the string representation of the class, which by default is `Namespace.ClassName`. So in that case, any two objects of the same type will be considered equal unless they've overridden `ToString()` – Rufus L Nov 05 '17 at 00:26
  • @RufusL: _"otherwise, if they aren't both null, it will call the Equals method of one against the other"_ -- this is not correct at all. Using the `==` and `!=` operators depends entirely on whether the class has declared an overload for those operators. By default, the `Equals()` method of the objects is _not_ called. – Peter Duniho Nov 05 '17 at 00:29
  • Oh, you're right, I was thinking of how `Equals` typically works. In the end it really depends on the classes you're comparing and what they've overridden. – Rufus L Nov 05 '17 at 00:32
  • @RufusL: _"So in that case, any two objects of the same type will be considered equal"_ -- no, not in the OP's case. He's explicitly said that using `ToString()`, his method works as expected, which it could do only if the type of the object has in fact overridden the `ToString()` method (otherwise it'd just always stop at the first element). – Peter Duniho Nov 05 '17 at 00:33
  • @peterduniho when the op says `ToString` works, they mean on all fields because the OP mentiins this *I have found a solution, by using toString() on all values.* At least that is my understanding. I doubt `ToString` is overridden. – CodingYoshi Nov 05 '17 at 00:47
  • @CodingYoshi: guess, we'll just have to disagree then. When someone tells me _"it works"_, without qualification, I assume they mean _'it works"_, without qualification. The OP is free to clarify...IME, it's unwise to assume anything beyond what has actually be _written_. – Peter Duniho Nov 05 '17 at 01:00
  • @peterduniho i am not disagreeing with you. Its not always about dis/agreements. I was just sharing that the OP kind of mentions that sentence. But overall it is not very clear. – CodingYoshi Nov 05 '17 at 01:14
  • @CodingYoshi: he also writes _" I have to compare temp.Data.ToString() and data.ToString()"_ which makes it very clear how he's using `ToString()`. – Peter Duniho Nov 05 '17 at 01:17
  • I'm still a little confused. I'm trying to look at differences between == (or !=, which I assume works similarly), object.referenceEquals(a,b), and .Equals(data). None turn out accurate results... – Birdman Nov 05 '17 at 02:07
  • I get that using == or != should be comparing their memory locations if I'm correct? So since some value I'm passing into the function is not at the same memory location as the node in the list, I get that's what they're comparing. But from what I can tell, using: temp.Data.Equals(data) Should be working...since I believe that is actually comparing their values and not the references. – Birdman Nov 05 '17 at 02:11
  • Nevermind, this is what I have used: while((!temp.Data.Equals(data)) && (temp.Next != null)) I forgot to add the ! before testing for the equality, so it wasn't working correctly. I will research more on these equality methods with objects, but for now this seems like a much better implementation than using .ToString(), which just seemed kinda of improper to me. Thanks. – Birdman Nov 05 '17 at 02:15

0 Answers0