equals

C# UnitTest - Assert.AreEqual() does not call Equals if the argument is null

Hello, i recently stumbled upon a seemingly weird behavior that Google completely failed to explain. using Microsoft.VisualStudio.TestTools.UnitTesting; class TestClass { public override bool Equals(object obj) { return true; } } [TestMethod] public void TestMethod1() { TestClass t = new TestClass (); Assert...

C# Overriding Equals using "as" and specialized Method for Correctness, Flexibility and Performance

I wondered about the best way to implement a correct, flexible and fast Equals in C#, that can be used for practically any class and situation. I figured that a specialized Equals (taking an object of the actual class as parameter) is needed for performance. To avoid code duplication, the general Equals ought to call the specialized Equa...

hashCode() method when equals() is based on multiple independent fields

i have a class whose equality is based on 2 fields such that if either one is equal then the objects of this type are considered equal. how can i write a hashCode() function for such an equals() so that the general contract of hashCode being equal when equals returns true is preserved? public class MyClass { int id; String name; ...

Most robust Equals implementation for custom classes for value equality in C#

Say I have a Point2 class, and I want to implement the following Equals: public override bool Equals ( object obj ) public bool Equals ( Point2 obj ) This is from the Effective C# 3 book: public override bool Equals ( object obj ) { // STEP 1: Check for null if ( obj == null ) { return false; } // STEP 3: e...

Any reason to prefer getClass() over instanceof when generating .equals()?

I'm using Eclipse to generate .equals() and .hashCode(), and there is an option labeled "Use 'instanceof' to compare types". The default is for this option to be unchecked and use .getClass() to compare types. Is there any reason I should prefer .getClass() over instanceof? Without using instanceof: if (obj == null) return false; i...

Finding out what caused equals() to return false

How can I find out what caused equals() to return false? I'm not asking about a sure-way, always right approach, but of something to aid in the development process. Currently I have to step into the equals() calls (usually a tree of them) until one of them is false, then step into it, ad nauseam. I thought about using the object graph,...

Is it possible to automatically handle List.Contains by comparing a property on the item?

Can we do something similar to List.Contains(myItem) in order to check if a property on an item in the list equals a property on myItem. (We have considered Contains and Exists, something like: if (orderLines.Contains(myLine)) { ... } but cannot get a simple expression.) We would like something as simple as the following: if (ord...

SQL JOIN: ON vs Equals

Is there any significant difference between the following? SELECT a.name, b.name FROM a, b WHERE a.id = b.id AND a.id = 1 AND SELECT a.name, b.name FROM a INNER JOIN b ON a.id = b.id WHERE a.id = 1 Do SO users have a preference of one over the other? ...

Operator Overloading with Interface-Based Programming in C#

Background I am using interface-based programming on a current project and have run into a problem when overloading operators (specifically the Equality and Inequality operators). Assumptions I'm using C# 3.0, .NET 3.5 and Visual Studio 2008 UPDATE - The Following Assumption was False! Requiring all comparisons to use Equals ra...

Value Equality with Bidirectional Association in C#

Background I have two objects which have bidirectional association between them in a C# project I am working on. I need to be able to check for value equality (vs reference equality) for a number of reasons (e.g to use them in collections) and so I am implementing IEquatable and the related functions. Assumptions I am using C# 3.0, ....

Comparing Character, Integer and similar types in Java: Use equals or ==?

I wanted to make sure about something in Java: If I have a Character or an Integer or a Long and those sort of things, should I use equals or is == sufficient? I know that with strings there are no guarantees that there is only one instance of each unique string, but I'm not sure about other boxed types. My intuition is to use equals, ...

Override == (equality) operator in NHibernate?

With NHibernate entities, you are meant to override Equals and GetHashCode. Is it a good idea to override the == operator to use the .Equals implementation also? ...

Assert.ReferenceEquals() Passes where Object.ReferenceEquals() returns 'false' in Visual Studio Test

In attempting to create an initial, failing unit test in Visual Studio Professonal 2008's test capabilities, I can't seem to get Assert.ReferenceEquals() to correctly fail when an object instance is not equal to a null reference. Note that object.ReferenceEquals() is correctly returning false for this same comparison. Here is my class c...

C# Assert.AreNotEqual versus Equals

While trying to verify to myself, that C# Equals for IEnumerables is a reference equals, I found something odd. With the following setup in NUnit var a = (IEnumerable<string>)(new[] { "one", "two" }); var b = (IEnumerable<string>)(new[] { "one", "two" }); this test Assert.IsFalse(a.Equals(b)); passes, while this test Assert.AreNot...

Overriding Equals and comparing to string.

I've defined a C# class with a string member. For all intents an purposes, think of this class as being a subclass of string (except that's not allowed). I'm using it to represent a strongly typed string field that matches a specific format (I've simplified this significantly). public class field { private readonly string m_field;...

C# difference between `==` and .Equals()

I have a condition in a silverlight application that comapres 2 strings, for some reason when I use '==' it returns false while .Equals() returns true. Here is the code : if (((ListBoxItem)lstBaseMenu.SelectedItem).Content.Equals("Energy Attack")) { // Execute code } if (((ListBoxItem)lstBaseMenu.SelectedItem).Content == "Energy At...

How can I check if two values in c# are equal? (Given any type of value)

I have this code here, which is intended to allow any type of arguments: public static void AreEqual(object expectedValue, object actualValue) { if (expectedValue == actualValue) { HttpContext.Current.Response.Write("Equal"); } else { HttpContext.Current.Response.Write("Not Equal"); } } If I call it using a couple of ints it do...

Java Reflection Equals Question

Hi Guys, Its been one of those days can someone help me out with this. I have 2 Stock Objects which I want to compare properties of at runtime. One instance is the cached instance, the other is a new stock instance which has just been delivered to my system, which may or may not equal the cached instance. See below where m is a method ...

C# How to select a Hashcode for a class that violates the Equals contract?

I've got multiple classes that, for certain reasons, do not follow the official Equals contract. In the overwritten GetHashCode() these classes simply return 0 so they can be used in a Hashmap. Some of these classes implement the same interface and there are Hashmaps using this interface as key. So I figured that every class should at l...

Why does my method throw a NoSuchMethodError?

I have implemented successfully the reflectionEquals method, with a list of excluded fields. return EqualsBuilder.reflectionEquals(this, obj, new String[] {"files", "notes", "status"}); However, I recently compiled my program on Java 1.5 and now I get the following error when the program hits the above line: java.lang.NoSuchMet...