equality

Comparing two collections for equality

I would like to compare two collections (in C#), but I'm not sure of the best way to implement this efficiently. I've read the other thread about Enumerable.SequenceEqual, but it's not exactly what I'm looking for. In my case, two collections would be equal if they both contain the same items (no matter the order). Example: collectio...

How do the equality (== double equals) and identity (=== triple equals) comparison operators differ?

Can you explain the difference between == and ===, giving some useful examples? ...

What is "Best Practice" For Comparing Two Instances of a Reference Type?

I came across this recently, up until now I have been happily overriding the equality operator (==) and/or Equals method in order to see if two references types actually contained the same data (i.e. two different instances that look the same). I have been using this even more since I have been getting more in to automated testing (comp...

How do you compare structs for equality in C?

How do you compare two instances of structs for equality in standard C? ...

Best practices for overriding isEqual: and hash

How do you properly override isEqual: in Objective-C? The "catch" seems to be that if two objects are equal (as determined by the isEqual: method), they must have the same hash value. The Introspection section of the Cocoa Fundamentals Guide does have an example on how to override isEqual:, copied as follows, for a class named MyWidget...

Javascript === vs == : Does it matter which "equal" operator I use?

I'm using JSLint to go through some horrific JavaScript at work and it's returning a huge number of suggestions to replace == with === when doing things like comparing 'idSele_UNVEHtype.value.length == 0' inside of an if statement. I'm basically wondering if there is a performance benefit to replacing == with ===. Any performance improv...

Poll: Correct behavior of equality when passed object does not match LHS type?

I asked a related question about findbugs, but let's ask a more general question. Suppose that I am working with an object-oriented language in which polymorphism is possible. Suppose that the language supports static type checking (e.g., Java, C++) Suppose that the language does not allow variance in parameters (e.g., Java, again...)...

What does System.Collections.Generic.Dictionary<T,T>.Equals actually do?

I ran into this today when unit testing a generic dictionary. System.Collections.Generic.Dictionary<int, string> actual, expected; actual = new System.Collections.Generic.Dictionary<int, string> { { 1, "foo" }, { 2, "bar" } }; expected = new System.Collections.Generic.Dictionary<int, string> { { 1, "foo" }, { 2, "bar" } }; Assert.AreEqu...

Elegant ways to support equivalence ("equality") in Python classes

When writing custom classes it is often important to allow equivalence by means of the == and != operators. In Python, this is made possible by implementing the __eq__ and __ne__ special methods, respectively. The easiest way I've found to do this is the following method: class Foo: def __init__(self, item): self.item = item...

How do I compare strings in Java?

I've been using the "==" operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug. Is "==" bad? When should it and should it not be used? What's the difference? Thanks ...

LINQ Select Distinct with Anonymous Types

So I have a collection of objects. The exact type isn't important. From it I want to extract all the unique pairs of a pair of particular properties, thusly: myObjectCollection.Select(item=>new { Alpha = item.propOne, Bravo = item...

Compare objects in LinkedList.contains()

I want to be able to have LinkedList.contains() return true for a custom comparator. Suppose that I have 1 LinkedList and 2 objects LinkedList<MyObject> myList = new LinkedList<MyObject>(); MyObject a = new MyObject("HELLO"); MyObject b = new MyObject("HELLO"); Technicaly, both objects are identical in terms of comparison (MyObject ...

Equality with Double.NaN

I have the following code... if (Price_Foreign != Double.NaN) { output.Append(spacer); output.Append(String.Format("{0,-10:C} USD",Price_Foreign)); } Which outputs: NaN USD What gives? I'm using Double.NaN to indicate that the value doesn't exist, and shouldn't be output. ...

php == vs === operator

What is the difference between == and === in php. I am unsure when to use both. Updated note: So that it shows up in StackOverflow search, the difference between == and === is the same as the difference between != and !==. ...

String equality vs equality of location

String s1 = "BloodParrot is the man"; String s2 = "BloodParrot is the man"; String s3 = new String("BloodParrot is the man"); System.out.println(s1.equals(s2)); System.out.println(s1 == s2); System.out.println(s1 == s3); System.out.println(s1.equals(s3)); // output true true false true Why don't all the strings have the same ...

C#: Checking if two Expression<Func<T, bool>> are the same

Is it possible to find out if two expressions are the same? Like given the following four expressions: Expression<Func<int, bool>> a = x => false; Expression<Func<int, bool>> b = x => false; Expression<Func<int, bool>> c = x => true; Expression<Func<int, bool>> d = x => x == 5; Then, at least as we ca...

== Operator and operands

I want to check whether a value is equal to 1. Is there any difference in the following lines of code Evaluated value == 1 1 == evaluated value in terms of the compiler execution ...

SQL equality/inequality comparison with nullable values

first take, kludge solution, sentinel approach(it's imperative that your program should not allow inputting of sentinel value): select coalesce(a, -2147483648) = coalesce(b, -2147483648) as is_equal -- a little postgresism let's say you forgot to block the sentinel value on your program, the user inputted -2147483648 on the B field...

VB.Net: test multiple values for equality?

How do you test multiple values for equality in one line? Basically I want to do if (val1 == val2 == val3 == ... valN) but in VB.Net. ...

c# object equality for database persistance

I want to learn how others cope with the following scenario. This is not homework or an assignment of any kind. The example classes have been created to better illustrate my question however it does reflect a real life scenario which we would like feedback on. We retrieve all data from the database and place it into an object. A object...