C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
When you allocate an object, you receive a reference containing a value indicating its memory location in addition to the object's data on the memory heap.
Tip: The object.ReferenceEquals method gives you the ability to determine if two objects are in the same memory location.
Example. Two StringBuilders are allocated upon the managed heap. This occurs in the new StringBuilder() statements. The identifiers builder1 and builder2 are reference variables. They contain values that points to those locations on the heap.
The first call to object.ReferenceEquals returns false because those two memory locations are different. The second call returns true because the references were set equal in an assignment.
Info: Assignments in the C# language perform a simple bitwise copy of the value. This makes the reference equivalent.
C# program that uses object.ReferenceEquals using System; using System.Text; class Program { static void Main() { // Test object.ReferenceEquals. StringBuilder builder1 = new StringBuilder(); StringBuilder builder2 = new StringBuilder(); Console.WriteLine(object.ReferenceEquals(builder1, builder2)); builder1 = builder2; Console.WriteLine(object.ReferenceEquals(builder1, builder2)); // Test object.ReferenceEquals on string literals. string literal1 = "a"; string literal2 = "a"; Console.WriteLine(object.ReferenceEquals(literal1, literal2)); literal1 = literal2; Console.WriteLine(object.ReferenceEquals(literal1, literal2)); } } Output False True True True
What about the second part of the example program where string literals are compared? In the compilation step of the C# language, string literals such as "a" are pooled and all references to a specific value will be equal.
Therefore: The two references literal1 and literal2 are equal. They point to the same memory location.
Performance. Is object.ReferenceEquals a slow or fast method call? I developed this console program that tests the performance of object.ReferenceEquals on a custom class. It sees how fast the call is when the objects are equal, and when they are not.
C# program that tests object.ReferenceEquals using System; using System.Diagnostics; class A { public int Id; } class Program { const int _max = 1000000000; static void Main() { bool same = true; A a1 = new A() { Id = 1 }; A a2 = same ? a1 : new A() { Id = 2 }; int x = 0; var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { if (object.ReferenceEquals(a1, a2)) { x++; } } s1.Stop(); var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { if (a1.Id == a2.Id) { x++; } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.Read(); } } Result when same equals true 0.32 ns [object.ReferenceEquals] 0.64 ns Result when same equals false 0.64 ns [object.ReferenceEquals] 0.64 ns
In this specific benchmark, object.ReferenceEquals was fast. It was a faster way of testing two objects for equality than comparing a unique Id field. Thus with objects similar to A, object.ReferenceEquals is not slow.
Summary. References in the C# language refer to a location in memory. By comparing references you can determine if the two variables have an identical location in memory. The object.ReferenceEquals method is not constantly used.
But: Its existence reveals the behavior of the virtual execution environment with regards to memory allocations and referential data.