views:

208

answers:

4

If I have an object of type MyBull and a List<MyBull> orig:

// Just an example
MyBull x = getMeTheObjectWithIdFromDB(9);

orig.add(x);

// Again same? data object
MyBull y = getMeTheObjectWithIdFromDB(9);

Why is this false then?

// This is false, even though all the properties
// of x and y are the same.
orig.Contains<MyBull>(y); 
+18  A: 

By default objects will expose reference based equality. If you want custom rules, such as equality based on id fields, you need to override the Equals and GetHashCode methods.

Sam
+1 Great! Now how do I go about doing this on a app thats is 75% coded. Could you please briefly explain the procedure in a paragraph or 2. Please add it into you main answer and not as a comment, Thanks Sam.
VoodooChild
This answer scored me another point in my "answer-a-C#-question-by-giving-a-Java-answer" game! :) Although I don't have much C# experience, the inner Java developer instantly blurted out "he forgot to implement `equals()` and `hashCode()`!" ;)
Adam Paynter
+2  A: 

Does your MyBull object implement IEquatable<T>.Equals? This method will determine the equality of two objects

requested by OP

Your MyBull class would implement IEquatable

public class MyBull : IEquatable<MyBull>

and then you would need to override the Equals method

public bool Equals(MyBull theOtherMyBull)

As David Neale mentions below, this is best used when you're comparing objects of the same type--which you are. Overriding Object.Equals and Object.GetHashCode will work too.

David
From MSDN (IEquatable<T>.Equals Method) - Indicates whether the current object is equal to another object of the same type. http://msdn.microsoft.com/en-us/library/ms131190.aspx
David Neale
That appears to be what is requested--using a generic List<MyBull>. @David Neale: sorry, misread your comment. It is simply clarification of my remark on "equality of two objects"
David
+1 No MyBull object doesn't implement IEquatable. Could you give a simple example in your answer and briefly explain the benefits please? Thanks
VoodooChild
+3  A: 

This is because the MyBull instances are being compared by reference. From the point of view from .NET, x and y are both different instances and therefore not Equal.

In order to get around this you will have to override the Equals and GetHashCode methods (which means you should probably implement IEquatable<MyBull> and override the == and != operators too).

casperOne
+1 Thanks. Now how do I go about doing this on a app thats is 75% coded. Could you please briefly explain the procedure in a paragraph or 2. Please add it into you main answer and not as a comment.
VoodooChild
+4  A: 

If you can use LINQ then you can

class Vessel
{
    public int id { get; set; }
    public string name { get; set; }
}

...

var vessels = new List<Vessel>() { new Vessel() { id = 4711, name = "Millennium Falcon" } };

var ship = new Vessel { id = 4711, name = "Millencolin" };

if (vessels.Any(vessel => vessel.id == ship.id))
    Console.Write("There can only be one!");
Jonas Elfström
+1 I like this approach, it is simple because I can compare any one or more property of my object. Thanks Jonas!
VoodooChild