Skip to main content

Why does default == implementation not call Equals?



Possible Duplicate:

Why ReferenceEquals and == operator behave different from Equals




The default implementation of == operator compares objects by references. So when you override Equals (which default behaviour is the same) you have to also specify == and != operators so that they call Equals (and make it in every class of hierarchy as == and != operators are not virtual).



My question is why it is so? Why does == and != compare objects by reference instead of using Equals? I guess there should be a reason for such a fundamental thing.



Update.



To comments: I assumed == should depend on Equals (but not vice versa) as you can override Equals in base class and use this implementation in derived classes automatically. It wouldn't work if Equals used == in its implementation, as == is not virtual.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. I believe the main reason is == is a static operator and can be called on null objects while Equals requires an instance.

    For example:

    Foo foo1 = null;
    Foo foo2 = null;

    Console.WriteLine(foo1 == foo2); // cannot use Equals

    ReplyDelete
  2. Object.ReferenceEquals is a static member that compares reference equality. Even value types are boxed before being passed to that method.

    What about Equals, it's a virtual method, which means it lets to a consumer to override a functionality.

    So default implementation of == behavior presumes default comparison(reference) is ok for you, if you need something specific, in this case framework provides you with a virtual method, which can be overriden.

    ReplyDelete
  3. The "reason" is because sometimes one needs to know if A is the same instance as B as opposed to whether or not they are merely "equal" to each other.

    For example, two objects being equal to each other may make sense for most of your business logic, however, you may also need to utilize some concurrency utilities which do atomic operations where the results are dependent on object identity, not equality.

    ReplyDelete
  4. The == has been around and used for reference since C, and it's integrated into the language syntax rather than having to reply on method invocation (Even if results are the same for both).

    Simply, because C# is not Objective C :)

    ReplyDelete
  5. In Java, it is sometimes nice to quickly determine whether or not two identifiers are equal by simply comparing the references. == has it's place. If you look at an IDE generated equals method, you will often find that the first comparison made is reference equality, after all, why check the fields if the object references are the same?

    ReplyDelete
  6. I would call it a feature. By reference two identical objects are still two separate objects. If you override Equals then you can determine if two objects are identical. Even if two objects are identical I also might want to test if the are the same object. I often have reason to override equals but never had the need to override == != (but that language provides that option).

    With string they override == and I don't like it. Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references (7.9.7 String equality operators). This makes testing for string equality more intuitive. See the problem this introduced. WPF ListBox Scroll to the bottom

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex