This article is in the continuation of series of articles regarding how Equality works in .Net, the purpose is to have the developers more clear understanding on how .Net handles equality for different types.
What we learned so far:
Following are the key points that we learned from the previous parts until now:
C# does not syntactically distinguish between value and reference equality which means it can sometimes be difficult to predict what the equality operator will do in particular situations.
There are often multiple different ways of legitimately comparing values. .Net addresses this by allowing types to specify their preferred natural way to compare for equality, also providing a mechanism to write equality comparers that allow you to place a default equality for each type
It is not recommended to test floating point values for equality because rounding errors can make this unreliable
There is an inherent conflict between implementing equality, type-safety and good Object Oriented practices.
Net provides the types equality implementation out of the box, few methods are defined by the .Net framework on the Object class which are available for all types.
By default the virtualObject.Equals method does reference equality for reference types and value equality for value types, but for value types it uses reflection which is a performance overhead for value types and any type can override Object.Equals method to change the logic of how it checks for equality e.g. String, Delegate and Tuple do this for providing value equality, even though these are reference types.
Object class also provides a static Equals method which can be used when there is chance that one or both of the parameters can be null , other than that it behaves identical to the virtual Object.Equals method.
There is also a static ReferenceEquals method which provides a guaranteed way to check for reference equality.
IEquatable interface can be implemented on a type to provide a strongly typed Equals method which also avoids boxing for value types. It is implemented for primitive numeric types but unfortunately Microsoft has not been very proactive implementing for other value types in the FCL( Framework Class Library ) .
For Value Types using == operator gives us the same result as calling Object.Equals but underlying mechanism of == operator is different in IL( Intermediate Language ) as compared to Object.Equals, so the Object.Equals implementation provided for that primitive type is not called, instead an IL instruction ceq gets called which says that compare the two values that are being loaded on the stack right now and perform equality comparison using CPU registers.
For Reference Types , == operator and Object.Equals method call both work differently behind the scenes which can be verified by inspecting the IL code generated. It also uses ceq instruction which do the comparison of memory addresses.
If you want to read the other parts published until now, you can read them here:
Story of Equality in .Net – Part 1
Story of Equality in .Net – Part 2
Story of Equality in .Net – Part 3
Story of Equality in .Net – Part 4
Story of Equality in .Net – Part 5
We will be looking at String type in this post that how Equality works for it. You might be aware that for strings the equality operator compares values not references which we had seen in the first post of this series. It is because String has overridden the implementation of Equals to behave in this manner.
We will investigate how == operator and Object.Equals method behave for equality checking.
Equality Operator and String:
Consider the following piece of code:
}[/code] The above code is very similar to what we have looked at before as well, but this time we have String type variables in place. We are creating a string and holding its reference in s1 variable and on next line we are creating copy of the string and holding its reference in another variable names s2 .
Then we are checking for reference equality for both the variables that are they both pointing to same memory location or not, then in next two lines we are checking the output of equality operator and Object.Equals method.
Now we will build the project and run it to see what it outputs on the console. The following is the output printed on console:
You can see that ReferenceEquals has returned false which means that both the string are different instances, but the == operator and Equals method have returned true , so it is clear that for Strings the equality operator does indeed test the value for equality not the reference exactly as Object.Equals does.
Behind the Scenes of Equality Operator for String
Let’s see how the equality operator is doing that. Now let’s examine the IL code generated for this example. For doing that, Open the Visual studio Developer Command Prompt , for opening it, go to Start Menu >> All Programs >> Microsoft Visual Studio >> Visual Studio Tools>> Developer Command Prompt
Type ildasm on the command prompt, this will launch the IL disassembler which is used to look at the IL code contained in an assembly, it is installed automatically when you install Visual Studio , so you don’t need to do anything for installing it.