Technology Microsoft Software & solutions

The Equals and ReferenceEquals Methods of the Object Object

The Object object is the root object of the entire .NET Framework. Since everything inherits from Object, the methods there can be found in all other objects, but they're usually overridden so the implementation changes a lot. There are only six public methods in Object. (They're introduced in the Quick Tip: The Object Object.) Two of the six are used for testing equality with other objects.
  • Equals


  • ReferenceEquals

The Equals method

The basic idea in using the Equals method is just what you might expect:

Dim a, b, c As Object a = "1" b = "1" c = "2" ' Returns True Console.WriteLine(a.Equals(b).ToString) ' Returns False Console.WriteLine(a.Equals(c).ToString)
Although you can create your own object (Class), inherit Object, and override Equals to make it do whatever you want (in your Class), Microsoft's rule book states that all implementations of Equals (including Microsoft's own implementations) have to follow these rules (I've added some simplification - see MSDN for the detailed version):
  • x.Equals(x) returns true
  • x.Equals(y) returns the same value as y.Equals(x)
  • x.Equals(y) returns true if both x and y are NaN
  • (x.Equals(y) && y.Equals(z)) returns true if and only if x.Equals(z) returns true
  • Successive calls to x.Equals(y) return the same value as long as the objects referenced by x and y are not modified
  • x.Equals(Nothing) returns false

But there can be some surprises. For example, this would seem to contradict the second rule:

Dim s As Short = 0 Dim l As Long = 0 Console.WriteLine(s.Equals(l)) ' False Console.WriteLine(l.Equals(s)) ' True
Microsoft's documentation for the Equals method specifies that it returns True only if "obj" is an instance of the object (Short or Long). In the first case, "l" is not an instance of Short, so Equals returns False. In the second case, VB.NET "casts" the "l" variable to a Short behind the scenes, then executes the Equals method and returns True.

This is a bit technical, but doubters can run ILDASM against the .exe after building a project with this code. The IL code for the first Equals is:

call instance bool [mscorlib]System.Int16::Equals(object)
The IL code for the second Equals is:

call instance bool [mscorlib]System.Int64::Equals(int64)
So, it's always a good idea to understand how Equals is implemented in the specific object you're using.

ReferenceEquals

This method is used to find out whether two instances of an object are the same instance. Sound confusing? It can be! The first question I had was, "When would you ever use such a method?" The documentation at Microsoft isn't much help. (This example is based on theirs, but I think it makes the point more clearly.)

Dim o As String = "ABCD" Dim p As String = "ABCD" Dim q As String = "EFGH" Console.WriteLine(Object.ReferenceEquals(o, p)) p = q Console.WriteLine(Object.ReferenceEquals(p, q)) Console.WriteLine(Object.ReferenceEquals(o, p)) ' True ' True ' False
This is super for demonstrating that VB.NET is effecient and doesn't waste memory resources for objects that don't need any new memory. Initially, "o" and "p" are the same instance. There is no reason to waste memory if there is no difference in the strings. But when a new string is assigned to one of them (p = q), those two strings are then the same instance, but the first two are not anymore.

I was not able to find an actual example of ReferenceEquals being used for something other than an example to demonstrate what VB.NET was doing with memory resources. (I did find one in a different tutorial, but I was able to get the same result with simpler code by eliminating the use of ReferenceEquals.) If someone knows of one, send me an email.
SHARE
RELATED POSTS on "Technology"
How to Stop Windows Genuine Notification Wizard
How to Stop Windows Genuine Notification Wizard
How to Use Windows XP Professional to Upgrade to Windows MCE 2005
How to Use Windows XP Professional to Upgrade to Windows MCE 2005
How do I Launch a Keyboard Macro?
How do I Launch a Keyboard Macro?
How to Tell If Windows Need Replacing
How to Tell If Windows Need Replacing
How to Uninstall a Malware Protector
How to Uninstall a Malware Protector
How to Get Rid of Newlines
How to Get Rid of Newlines
PC Diagnostic Help
PC Diagnostic Help
How to Disable Remote Devices in Group Policy
How to Disable Remote Devices in Group Policy
I Can't Use the Volume Buttons on My Headphones on My XP PC
I Can't Use the Volume Buttons on My Headphones on My XP PC
How to Restore Your PC to One Month Ago in Vista
How to Restore Your PC to One Month Ago in Vista
How to Unlock Volume Icons in the Notification Area of Windows Vista
How to Unlock Volume Icons in the Notification Area of Windows Vista
How to Fix Run Dll Errors
How to Fix Run Dll Errors
How To Run In Safe Mode In Windows Vista
How To Run In Safe Mode In Windows Vista
Microsoft Windows 2000 Registry Repair
Microsoft Windows 2000 Registry Repair
How to Unzip Files in Windows Vista Home Basic
How to Unzip Files in Windows Vista Home Basic
How to Enter Safe Mode on an Asus Eee PC
How to Enter Safe Mode on an Asus Eee PC
How to Open an HP Computer
How to Open an HP Computer
How to Identify What Network Adapters Are Installed on Windows Vista With No Manufacturer Listed
How to Identify What Network Adapters Are Installed on Windows Vista With No Manufacturer Listed
How to Fix Virtual Memory That Is Running Low
How to Fix Virtual Memory That Is Running Low
The Windows Update Won't Work After Installing XP SP3
The Windows Update Won't Work After Installing XP SP3
How to Access the BIOS Menu
How to Access the BIOS Menu
List of Computer Database Registry Programs
List of Computer Database Registry Programs
How to Boot a Computer to VMware
How to Boot a Computer to VMware
How to Raise Data Recovery for XFS
How to Raise Data Recovery for XFS
How to Clear the Windows ARP Cache
How to Clear the Windows ARP Cache

Leave Your Reply

*