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
187
votes
22 answers

Difference between null and empty ("") Java String

What is the difference between null and the "" (empty string)? I have written some simple code: String a = ""; String b = null; System.out.println(a == b); // false System.out.println(a.equals(b)); // false Both statements return false. It seems,…
Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
65
votes
5 answers

IEqualityComparer that uses ReferenceEquals

Is there a default IEqualityComparer implementation that uses ReferenceEquals? EqualityComparer.Default uses ObjectComparer, which uses object.Equals(). In my case, the objects already implement IEquatable, which I need to ignore and…
Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
35
votes
8 answers

why equals() method when we have == operator?

When i see the implementation of equals() method it does nothing but same as what == does. So my question is what was the need to have this as separate method when we have == operator which does the same work?
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
26
votes
3 answers

C# Differences between operator ==, StringBuilder.Equals, Object.Equals and Object.ReferenceEquals

I have a question about Object.Equals and Equals(object). My sample code is below: class Program { static void Main(string[] args) { var sb1 = new StringBuilder("Food"); var sb2 = new StringBuilder("Food"); …
amitabha
  • 646
  • 1
  • 9
  • 20
18
votes
3 answers

What does this overload mean?

Can someone explain me what does this overload mean? public static bool operator ==(Shop lhs, Shop rhs) { if (Object.ReferenceEquals(lhs, null)) { if (Object.ReferenceEquals(rhs, null)) { return true; } …
Madness
  • 225
  • 1
  • 5
15
votes
5 answers

ReferenceEquals working wrong with strings

Why in this situation ReferenceEquals method of object behaves differently? string a= "fg"; string b= "fg"; Console.WriteLine(object.ReferenceEquals(a, b)); So in this situation it's get a result true. In case, it compares values of my strings and…
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
11
votes
1 answer

Combo of IdentityHashMap and WeakHashMap

I need a Map implementation that shares properties of both IdentityHashMap and WeakHashMap (reference equality instead of equals() and weak references on keys). What implementation do you recommend (it has to work on Android)?
zduny
  • 2,481
  • 1
  • 27
  • 49
9
votes
1 answer

Is the 'Is' VB.NET keyword the same as Object.ReferenceEquals?

Is the Is VB.NET keyword the same as Object.ReferenceEquals?
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
8
votes
4 answers

Physical identity based alternative to Hashtbl.hash

I'm trying to derive a Graphviz file describing a structured value. This is for diagnostic purposes so I want my graph to mirror the actual structure in memory as closely as possible. I'm using the below to map values to Graphviz vertices so that…
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
6
votes
4 answers

Is it possible to create a string that's not reference-equal to any other string?

It seems like .NET goes out of its way to make strings that are equal by value equal by reference. In LINQPad, I tried the following, hoping it'd bypass interning string constants: var s1 = new string("".ToCharArray()); var s2 = new…
millimoose
  • 39,073
  • 9
  • 82
  • 134
5
votes
2 answers

Object.Equals is virtual, but Object.operator== does not use it in C#?

I got hit by a strange "asymmetry" in C# that I do not really understand. See the following code: using System; using System.Diagnostics; namespace EqualsExperiment { class Program { static void Main(string[] args) { …
wigy
  • 2,174
  • 19
  • 32
5
votes
3 answers

Why string interned but has different references?

string s1 = "abc"; string s2 = "ab"; string s3 = s2 + "c"; Console.WriteLine(string.IsInterned(s3)); // abc Console.WriteLine(String.ReferenceEquals(s1, s3)); // False I just cannot understand why s3 interned, but ReferenceEquals was…
Lei Kan
  • 487
  • 7
  • 20
5
votes
2 answers

How to use Object.GetHashCode() on a type that overrides GetHashCode()

I have a class A that implements IEquatable<>, using its fields (say, A.b and A.c) for implementing/overriding Equals() and overriding GetHashCode(), and everything works fine, 99% of the time. Class A is part of a hierarchy (class B, C) that all…
Jimmy
  • 5,131
  • 9
  • 55
  • 81
5
votes
6 answers

How use of == operator brings in performance improvements compared to equals?

In Effective JAVA by Joshua Bloch, when I was reading about static factory methods , there was a statement as follows The ability of static factory methods to return the same object from repeated invocations allows classes to maintain strict…
Harish Kayarohanam
  • 3,886
  • 4
  • 31
  • 55
5
votes
0 answers

issue with ReferenceEquals string comparison in Mono

I'm creating a Uri object in Mono (under unix system), this is very simple(like in VStudio/Windows): new Uri("http://my.url_here.com/"). Then I'm creating another system object which use the Uri: HttpSelfHostConfiguration(). In the source code of…
user810917
  • 241
  • 4
  • 12
1
2 3