请选择 进入手机版 | 继续访问电脑版

技术控

    今日:0| 主题:61300
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Story of Equality in .Net - Part 6

[复制链接]
枕梦 发表于 2016-10-2 23:05:32
418 8
Background:

  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 virtual Object.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<T> 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     
   Introduction:

     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:
  1. class Program
  2. {
  3.     static void Main(String[] args)
  4.     {
  5.         string s1 =  "Ehsan Sajjad";
  6.         string s2 = String.Copy(s1);
  7.         Console.WriteLine(ReferenceEquals(s1, s2));
  8.         Console.WriteLine(s1 == s2);
  9.         Console.WriteLine(s1.Equals(s2));
  10.             
  11.         Console.ReadKey();
  12.     }
  13. }
复制代码
  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:
   

Story of Equality in .Net - Part 6

Story of Equality in .Net - Part 6-1-技术控-particular,different,difficult,sometimes,regarding

   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
   

Story of Equality in .Net - Part 6

Story of Equality in .Net - Part 6-2-技术控-particular,different,difficult,sometimes,regarding

   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.
   

Story of Equality in .Net - Part 6

Story of Equality in .Net - Part 6-3-技术控-particular,different,difficult,sometimes,regarding

   Click the File Menu to open the menu, and click the Open Menu Item which will bring up the window to browse the executable that we want to disassemble.
   

Story of Equality in .Net - Part 6

Story of Equality in .Net - Part 6-4-技术控-particular,different,difficult,sometimes,regarding

  Now navigate to the location where your application executable file is located and open it.
   

Story of Equality in .Net - Part 6

Story of Equality in .Net - Part 6-5-技术控-particular,different,difficult,sometimes,regarding

  This will bring up the code of the assembly in hierarchical form, as we have multiple classes written in the assembly, so it has listed down all the classes.
   Now the code which we want to explore is in the Main Method of the Program class so navigate to the Main method and double click it to bring the IL code for it.
   

Story of Equality in .Net - Part 6

Story of Equality in .Net - Part 6-6-技术控-particular,different,difficult,sometimes,regarding

      The IL code for main looks like this:     
   

Story of Equality in .Net - Part 6

Story of Equality in .Net - Part 6-7-技术控-particular,different,difficult,sometimes,regarding

   IL for String override of Equals Method

     First let;s look at the IL generated for s1.Equals(s2) , and there are no surprises as it is calling Equals method but this time it is calling the method implementation of IEquatable<string> which takes a string as argument, not the Object.Equals override is being called, because the compiler found a better match for the string argument that was supplied. See the picture below:
   

Story of Equality in .Net - Part 6

Story of Equality in .Net - Part 6-8-技术控-particular,different,difficult,sometimes,regarding

  IL for == operator for String

   Now let’s examine what is the IL generated for the string equality checking done using equality operator, so we can see the now there is no ceq instruction being called which we saw in previous posts that for value types and reference types that instruction is executed when we check for equality using == operator , but for String we have call to a new method named op_equality(string, string) which takes two string arguments, we have not seen this kind of method before, so what it is actually?  
   The answer is it is the overload of C# equality operator (==) which is provided by String class. In C# when we define a type, we have the option to overload the equality operator for that type, for  example , for Person class which we have been seeing in previous examples will look like following, if we overload the == operator for it:
  1. public class Person
  2. {
  3.     public int Id { get; set; }
  4.     public string Name { get; set; }
  5.     public static bool operator == (Person p1, Person p2)
  6.     {
  7.         bool areEqual = false;
  8.         if (p1 == null || p2 == null)
  9.             areEqual = false;
  10.         if(p1 == null  && p2 == null )
  11.             areEqual = true;
  12.         if (p1.Id == p2.Id)
  13.             areEqual = true;
  14.         else
  15.             areEqual =  false;
  16.         return areEqual;
  17.     }
  18. }
复制代码
  So the above code is pretty simple, We have declared an operator overload which would be a static method, but the thing to notice here is that the name of the method is operator == The similarity of declaring operator overload with static method is not a con-incidence, actually it is compiled as a static method by the compiler, because we know and had been discussed before that IL (Intermediate Language) has no concept of operators, events etc , it only understands fields and methods, so operator overload can only exist as a method which we observed in IL code above, the overload operator code is turned by compiler in to a special static method called op_Equality() .
  First it is checking if any of the passed instances is null then they are not equal, then we see if both are null then obviously both references are equal so it will return true, and next it checks if Id property of both references is equal then they are equal, otherwise they are not equal.
  This way we can define our own implementation for our custom types according to the business requirements. As we discussed earlier that equality of two objects is totally dependent on the business flow of the application, so two objects might look equal to someone but not equal to some other according to their business logic.
   This makes one thing clear that Microsoft has provided == operator overload for String class, and we can even see that if we peek in to the source code of String class in Visual Studio by using Go to Definition , which would be like:
   

Story of Equality in .Net - Part 6

Story of Equality in .Net - Part 6-9-技术控-particular,different,difficult,sometimes,regarding

   
   In the above snapshot, we can see that there are two operator overload one for equality and the other is inequality operator which works exactly the same way but the negation of equality operator output.  
   
  
  Summary

  
       
  • We have now enough understanding of what C# Equality operator does in the case of Reference Types . Following the thing that we need to keep in mind:   
         
    • If there is an overload for the equality operator for the type being compare, it uses that operator as a static method .   
    • If there is no overload of operator for reference type, the equality operator compares the memory addresses using ceq instruction.   
       
  • One thing to note is that Microsoft made sure that == operator overload and Object.Equals override   always give the same result even though they are in fact different methods. So that is an important thing we need to keep in mind when we start implementing our own Equals override , we should also take care of the equality operator as well, otherwise our type will end up giving different result using Equals override and equality operator which would be problematic for consumers of the type. We will be seeing in another post how we can override Equals method in a proper way.   
  • If we are changing how Equality works for a type, we need to make sure we provide implementation for both Equals override and == operator overload   so that they both give the same result, and that’s obvious otherwise it would be confusing for other developers who will be using our implemented type .  
Tammy 发表于 2016-10-3 04:51:40
有钱任性
回复 支持 反对

使用道具 举报

邓雪 发表于 2016-10-5 02:17:06
帮不上什么忙,把帖子顶上去吧!
回复 支持 反对

使用道具 举报

fengzhi99 发表于 2016-10-5 21:57:21
低端粗俗甩节操
回复 支持 反对

使用道具 举报

aakendy 发表于 2016-11-6 13:05:17
占位编辑
回复 支持 反对

使用道具 举报

xgang12315 发表于 2016-11-8 20:08:51
楼下有什么好吐槽的么?
回复 支持 反对

使用道具 举报

骑着母猪去约会 发表于 2016-11-9 04:05:05
喜欢的摇,不喜欢的滚。
回复 支持 反对

使用道具 举报

刘子依 发表于 2016-11-10 23:04:49
前排,留名!
回复 支持 反对

使用道具 举报

老乡网 发表于 2016-11-16 18:52:13
楼主,你说,你几个意思?
回复 支持 反对

使用道具 举报

我要投稿

回页顶回复上一篇下一篇回列表
手机版/c.CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 | 粤公网安备 44010402000842号 )

© 2001-2017 Comsenz Inc.

返回顶部 返回列表