equality

Composition vs Inheritance for Equality & Hashcode providers

When comparing entities and aggregate roots I use an ABC, which I borrowed from Oren Eini: Generic Entity Equality. For value objects I was equally ingenious. I used Jimmy Bogard’s Value Object ABC: Generic Value Object Equality Now my question is; should I be favouring inheriting these ABCs or should I perhaps be using the generic equa...

Reference equality performance difference? ((object)obj1 == (object)obj2) vs. object.ReferenceEquals( obj1, obj2 )

Is there extra overhead in using the object.ReferenceEquals method verses using ((object)obj1 == (object)obj2)? In the first case, there would be a static method call involved, and in both cases some form of casting to an object would be involved. Even if the compiler balances out those methods, what about inequality? (object)obj != n...

What is the best way to check two List<T> lists for equality in C#

There are many ways to do this but I feel like I've missed a function or something. Obviously List == List will use Object.Equals() and return false. If every element of the list is equal and present in the same location in the opposite list then I would consider them to be equal. I'm using value types, but a correctly implemented Dat...

set equality in linq

I have two lists A and B (List). How to determine if they are equal in the cheapest way? I can write something like '(A minus B) union (B minus A) = empty set' or join them together and count amount of elements, but it is rather expensive. Is there workaround? ...

Should I use a concatenation of my string fields as a hash code?

I have an Address class in C# that looks like this: public class Address { public string StreetAddress { get; set; } public string RuralRoute { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; } public string PostalCode { get; set; } }...

How could this simple pointer equality test fail?

void FileManager::CloseFile(File * const file) { for (int i = 0; i < MAX_OPEN_FILES; ++i) { if ((_openFiles[i] == file) == true) { _openFiles[i] == NULL; } } ... _openFiles is a private member of FileManager and is just an array of File *'s When the exact same test is performed in the Immediate window i get a result of 1!?!...

I wanna have an Extension method "Equals" between 2 byte arrays.

Hi! I am doing some byte[] comparisons. I tried == but this is just like the base Equals, which: byte[] a = {1,2,3}; byte[] b = {1,2,3}; bool equals = a == b; //false equals = a.Equals(b); //false I tried to add an extension method, but since the overloaded base class' Equals takes the same arguments, it goes to the base method rath...

What's wrong with using == to compare floats in Java?

According to this java.sun page == is the equality comparison operator for floating point numbers in Java. However, when I type this code: if(sectionID == currentSectionID) into my editor and run static analysis, I get: "JAVA0078 Floating point values compared with ==" What is wrong with using == to compare floating point...

Implementing -hash / -isEqual: / -isEqualTo...: for Objective-C collections

Note: The following SO questions are related, but neither they nor the linked resources seem to fully answer my questions, particularly in relation to implementing equality tests for collections of objects. Best practices for overriding -isEqual: and -hash Techniques for implementing -hash on mutable Cocoa objects Background NSObj...

How to do equality comparison in SQL with C#-like behavior?

How to compare equality of value in SQL with null? For those familiar with C#, here are the results of comparing nullable values: null == null : true null == john : false null == paul : false john == null : false john == john : true john == paul : false paul == null : false paul == john : false paul == paul : true The easiest solutio...

Determine equality of Datetime values with minute precision within LINQ

I need to compare two datetime values to determine equality(exactly the same),using minute precision.Would this be the best way to do it? My dates could have seconds and milliseconds, but i want to consider only down till minutes. where (Math.Abs(datetime1.Subtract(datetime2).TotalMinutes) == 0) ...

Hashsets and different instances of class with all members identical

Let's say I have a class called myclass. In my code I have two instances of myclass, myclass1 and myclass2. Everything about them is (public and private) properties are identical. If I try to add both of them to a HashSet will it add both or only one? If it adds both and I don't want it to, can I overidde equals in the myclass definiti...

C# - List<T>.Remove() always deletes the first object on the list

Working in Visual Studio 2008 (C#)... I use a List collection to store instances of my custom class (Shift). I want to delete a certain shift from the list by using the Remove method. But List.Remove() always deletes the first item it finds. I've implemented the IComparable interface for my Shift, I thought this would be enough, then...

Javascript : {}==false and false=={}

In Firefox 3.5, I type this in the Firebug console : false=={} // => evals to false {}==false // syntax error What is the explanation for this ? ...

Comparing two List<string> for equality

Other than stepping through the elements one by one, how do I compare two lists of strings for equality (in .NET 3.0): This fails: // Expected result. List<string> expected = new List<string>(); expected.Add( "a" ); expected.Add( "b" ); expected.Add( "c" ); // Actual result a...

What is a good example of an __eq__ method for a collection class?

I'm working on a collection class that I want to create an __eq__ method for. It's turning out to be more nuanced than I thought it would be and I've noticed several intricacies as far as how the built-in collection classes work. What would really help me the most is a good example. Are there any pure Python implementations of an __eq...

IEqualityComparer for Value Objects

I have an immutable Value Object, IPathwayModule, whose value is defined by: (int) Block; (Entity) Module, identified by (string) ModuleId; (enum) Status; and (entity) Class, identified by (string) ClassId - which may be null. Here's my current IEqualityComparer implementation which seems to work in a few unit tests. However, I d...

Java: clone() and equality checks

Perhaps I don't understand how clone() works. Shouldn't the return value equal the caller? int[] nums = new int[] {0, 1, 2}; int[] list = nums.clone(); nums.equals(list); //returns false. Why? for (int ket = 0; ket < list.length; ket++) { System.out.println(list[ket] == nums[ket]); //prints out true every time } list == nums /...

.NET delegate equality?

I think this is the question, anyway. I am using a RelayCommand, which decorates an ICommand with two delegates. One is Predicate for the _canExecute and the other is Action for the _execute method. ---Background motivation -- The motivation has to do with unit testing ViewModels for a WPF presentation. A frequent pattern is that I hav...

wpf mvvm equality

I would like to use an MVVM in a WPF project I'm working on, including the use of RelayCommands (aka DelegateCommands). I'm running into an interesting but frustration problem in implementing equality for my ViewModels, outlined here. I have a base class in my ViewModel hierarchy which examines all properties reflectively as part of its ...