Re: string == and Equals

Tech-Archive recommends: Fix windows errors by optimizing your registry



On Thu, 24 Jan 2008 10:59:00 -0800, Jon Skeet [C# MVP] <skeet@xxxxxxxxx> wrote:

And please, remind me again, why is it that the == operator doesn't just
automatically use Object.Equals()?

I'm sure there's a good reason, but it's escaping me at the moment. It
seems pretty relevant to the question though.

Good question. There are any number of interesting things you can do
with operator overloading, although I'm not sure of the merit of them.

I thought in C# you couldn't overload the == operator. That was the source of my confusion.

I don't know where I got that idea (maybe because the assignment operator can't be overloaded), but yes...I agree that given that == can be overloaded, you can't rely on it always calling Object.Equals(). I was under the mistaken impression that this was something the compiler could have implemented.

I guess that's the "good reason" to which I referred. :)

Pete
.



Relevant Pages

  • Re: Hashtable
    ... It is occasionally pointed out that although Java claims operator ... overloading is bad (which is one of the reason Java doesn't allow operator ... overloading), it's somewhat hypocritical that Java overloads the + operator ... So coming up with a new operator for string concatenation might be seen ...
    (comp.lang.java.programmer)
  • Re: Python and Flaming Thunder
    ... catch statement is that the error path is separate from the normal ... It also looks like an overloading of catch, ... exception-handling mechanism. ... equal to the l-value at the end, that's fine, but there's no reason ...
    (comp.lang.python)
  • Re: Enumeration literals and their associated functions
    ... because the standard overloading rules apply. ... Yeah, that's probably the reason. ... But it would sure be nice if enumeration literals were overloadable. ... one might want to change type Base above (for various good ...
    (comp.lang.ada)
  • Re: Overloading a method with different return types but the same parameter lists
    ... > How can compiler know which version you would like to use if you call ... The other reason is that passing a by-result overloaded ... void Process; ... and the benefit of by-result overloading is gone. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Object REXX is a s***...
    ... overloading, but I don't find gratuitous overloading. ... Operator overloading seems to invite abuse for some reason. ... I can see its utility eg. for matrix math, where the overloaded operators are intuitive and do what most people would expect them to do. ... In my experience, too many programmers do not have the discipline not to abuse certain language features, so the length of rope provided by the language ought to be limited;) ...
    (comp.os.os2.programmer.misc)