Questions tagged [equality]

Equality is a relationship between two or more items or variables or objects that exists if (1) the items are the same item, variable, or object or (2) the items are different items, variables or objects but they have the same value. This tag should generally be used with programming language specific tags as well as other contextual tags such as database system. The post should include as much context about the equality test as is possible.

This tag is typically used for questions concerning how to determine if two objects or variables are either the same object or different objects with the same value. Questions almost always require a programming language tag and other tags for context.

Programming languages represent variables, objects, or other storage containers using different underlying mechanisms for the storage of the values of variables and objects. A test for equality is to compare some characteristic of two variables or objects to determine if the characteristic is the same.

This characteristic is typically either (1) the storage identifier such as a reference handle, memory address, etc. when checking if the two are the same object or (2) the characteristic is the value of the objects retrieved using a storage identifier when checking if the value of the two are the same value.

Different programming languages and different operating environments have differences in how equality is tested and what operators are used to express a test for equality. In some cases various conversions from underlying storage representations may be involved in testing for equality.

See What is the difference between equality and equivalence? as well as Equivalence relation and natural ordering on a class in Java

2095 questions
1
vote
2 answers

Can I test two ArrayList (C#) instances for value equality?

I wrote a small helper method contentsToArrayList. If I pass it an object which implements ICollection, it returns an ArrayList containing the same elements as the original object. If I pass it another object, it returns an ArrayList containing the…
Rumi P.
  • 1,688
  • 3
  • 23
  • 31
1
vote
2 answers

Is there any way to tell if two strings share memory in Go?

In Go, strings are stored internally as the C-struct: struct String // This is C code (not Go) { byte* str; int32 len; }; Let's say I have the following variables: a0 := "ap" // This is Go code a1 := "ple" b0 := "app" b1 := "le" a := a0 + a1 b…
Matt
  • 21,026
  • 18
  • 63
  • 115
1
vote
1 answer

How to check if two dictionaries contain the same values?

I've got this, but it's so short I'm nearly sure I'm missing something: public static bool ValueEquals (this IDictionary source, IDictionary toCheck) { if (object.ReferenceEquals(source, toCheck)) …
It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
1
vote
1 answer

NDepend rule to warn if objects of a given type are compared using ==

as the title says: I need a NDepend rule (CQLinq) for C#/.net code, that fires whenever instances of a given type are compared using == (reference comparison). In other words, I want to force the programmer to use .Equals. Note that the type in…
1
vote
1 answer

value of CustomEquality and CustomComparison

I understand the value of asserting [] This statically forces equality and comparison constraints to be derived structurally, and have a nice side effect to warn if it can not Similarly…
nicolas
  • 9,549
  • 3
  • 39
  • 83
1
vote
1 answer

Call to == does not opt to equals

I have the following class: abstract class IRDMessage extends Ordered[IRDMessage] { val messageType: MessageType.Type val timestamp: DateTime val content: AnyRef def compare(that: IRDMessage): Int = { val res = timestamp compareTo…
user221218
  • 143
  • 1
  • 1
  • 6
1
vote
2 answers

Linq, select differences between two lists, select same between same list, and select duplicates same list

I want to know if it's possible in LINQ to achieve something along the lines of: newList: { [1], [2] } oldList: { [2], [3], [4], [5] } resultantList = { [1], [2, 2], [3], [4], [5] } Ok that was an oversimplification. Lets say: Class A { …
zman
  • 333
  • 1
  • 2
  • 9
1
vote
1 answer

PHP: Why won't the program compare values as equal when it prints them with equal values

I have this code for a system: //get new data from text fields $i = 0; $data = array(); while ($i < 7){ $data[$i] = $_POST['t'.$i]; $i++; } //get the ID to use in the MySQL database query $id=$data[0]; //get the existing integer from MySQL…
1
vote
2 answers

.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…
Berryl
  • 12,471
  • 22
  • 98
  • 182
1
vote
2 answers

How does JavaScript do the type conversion for == ?

In the below code, a != b when compared with ==. My initial thought is that JavaScript would use the same conversion for parseFloat as it would with ==. Can anyone explain what actually happens as I'm a little confused by this. b = 129 when parsed,…
Yatrix
  • 13,361
  • 16
  • 48
  • 78
1
vote
1 answer

Odd equality result with a HashSet of an Enum type?

I have two hash sets that I've constructed in different ways that contain all the enum values. setWithAllEnums.Equals(setToTest); // Returns false !(setWithAllEnums.Except(setToTest).Any()); // Returns True Why are these not…
Fredrick
  • 1,210
  • 2
  • 15
  • 24
1
vote
1 answer

Comparing a static functions equality in javascript?

I am working on a little project and one of the objects for the project can include update functions being added to an array that is a property of the object. Example, /* Add an update function to the layer @param function {update} The…
GriffLab
  • 2,076
  • 3
  • 20
  • 21
1
vote
4 answers

Python If Statement Never Evaluates to True

I apologize if my questions seem trivial. I would rather ask this in a chat room; however, my reputation is too low at the moment, so I am unable to ask anything in the Python chat room. I am currently learning Python for a class and the teacher…
jesiKat
  • 37
  • 7
1
vote
7 answers

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…
djskinner
  • 8,035
  • 4
  • 49
  • 72
1
vote
7 answers

Understanding == and equals

I learned that == checks if the references being compared are the same,, while .equals() compares the two states. So then why can we use == inside the .equals() method? Like for example: public boolean equals(Object o){ //cast o to…
Deniz Cetinalp
  • 901
  • 1
  • 18
  • 34