Questions tagged [referenceequals]

ReferenceEquals is a method in C# used to compare references of object types. By default this is the way objects are compared in most OOP programming languages.

33 questions
5
votes
2 answers

Reference equality for java.lang.String in Scala

One would expect that even though strings are immutable, value-equality and reference-equality would not be the same for java.lang.String objects in Scala. This means that two string-holding vals should not be reference-equal even when their strings…
Oleg Mirzov
  • 155
  • 3
  • 8
4
votes
2 answers

When are physically distinct values created in OCaml?

I'm trying to understand what the physical equality operators (Pervasives.(==) and Pervasives.(!=)) mean in OCaml. The language manual says that the expression "" is a "constant", not an "expression": 6.5 Constants constant ::== ...…
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
3
votes
6 answers

Extend "object" with a null check more readable than ReferenceEquals

I tried to extend "object" to allow a more readable check if an object is null. Now, object.ReferenceEquals really checks for a null object, (the rare times it will not apply are since the operator == can be overridden. the object.Equals(null)…
Joezer
  • 625
  • 1
  • 9
  • 20
2
votes
1 answer

Why sometimes 2 objects reference the same but not always

Following the last answer : Recursive method to convert flat collection to hierarchal collection? I want to use the same method CreateTree but with another object than Hierarchy: ItemNode: public class ItemNode { public string Id { get; set; } …
Florian
  • 4,507
  • 10
  • 53
  • 73
2
votes
1 answer

How to check equality of boxed object of value types when types are different but compatible to compare the values

When we box two value types(which are different types but compatible to compare the values eg: int and short) and try to call Equals method on that gives false even the values are same. Case 1: int a = 5; short b = 5; var ob_a = (object) a; var…
vrnithinkumar
  • 1,273
  • 1
  • 11
  • 29
2
votes
3 answers

In `equals(T value)`, must T be Object, or can it be like City, etc?

I'm trying to understand the equals() method better. All examples I've seen do something like: public class City { public boolean equals(Object other) { if (other instanceof City && other.getId().equals(this.id)) { …
Amy B
  • 17,874
  • 12
  • 64
  • 83
2
votes
2 answers

Why would I ever want to do object.ReferenceEquals(null, this) in Equals override?

Consider the following code that I was reviewing: public override bool Equals(object other) { return !object.ReferenceEquals(null, this) && (object.ReferenceEquals(this, other) || ((other is MyType) &&…
Abel
  • 56,041
  • 24
  • 146
  • 247
1
vote
1 answer

Why is ReferenceEquals still used with value types?

There is a nice explanation why object.ReferenceEquals(this, obj) must not be used for value types: When comparing values using ReferenceEquals, if objA and objB are value types, they are boxed before they are passed to the ReferenceEquals method.…
momo
  • 3,313
  • 2
  • 19
  • 37
1
vote
4 answers

What is the difference between a==b and a.Equals(b) in the context of value and reference types?

I've come across this question quite a few times, and while the answers make sense, i wanted to check it out myself with a simple console app. class Program { static void Main(string[] args) { // Case 1 : FOR REFERENCE TYPES…
PreethaA
  • 905
  • 12
  • 13
1
vote
2 answers

Is this way of using ReferenceEquals correct

I am doing some code review and I stopped on the following construct. Is this the Correct way of using ReferenceEquals to check if a method returned actually the same object that was passed as an argument or a new one? int x = 5; Foo f = new…
Sebastian Widz
  • 1,962
  • 4
  • 26
  • 45
1
vote
1 answer

Overriding .equals() method (== returned true while comparing Strings)!

public class Employee { private String firstName; private String lastName; private int age; public Employee(String firstName, String lastName, int age) { super(); this.firstName = firstName; this.lastName = lastName; this.age =…
user104309
  • 690
  • 9
  • 20
1
vote
3 answers

Collection using reference equality

In Java is it possible to create HashMap that uses reference equality (i.e. ==) instead of the equals() method?
zduny
  • 2,481
  • 1
  • 27
  • 49
0
votes
1 answer

C# issue storing objects into Dictionaries

This seems to be simple, but I'm not sure what I'm doing wrong... I wrote the following Class, which I further use it in a Dictionary object, as its .Value: public class StorageList { static List listForStorage = new(); …
roccaforte
  • 81
  • 5
0
votes
2 answers

How to do real identity equality check for string in Javascript

I have seen many explanations of javascript identity equal operator ===, but seems they are not quite accurate as what we understand of identity equality in other languages such as Java. Seems for base types (such as number, string), === return true…
Jianwu Chen
  • 5,336
  • 3
  • 30
  • 35
0
votes
1 answer

Comparing objects with same type evaluates for false

I have this code in an if statements which evaluates to false and I don't have any clue why && (typeof(TResponse).Equals(typeof(MediatorResponse))) used Equals as I intend to compare by reference I tried to put them in watch and this is my…