Re: compare two structs via ==
- From: Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
- Date: Thu, 7 Jun 2007 20:46:19 +0100
Peter Duniho <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote:
On Thu, 07 Jun 2007 11:37:47 -0700, Jon Skeet [C# MVP] <skeet@xxxxxxxxx>
wrote:
but you *overload* the == operator by providing different signatures,
eg
bool operator ==(string x, string y)
bool operator ==(DateTime x, DateTime y)
You seem to be in agreement with me. Mark's claim was that it's only
"overloading" when the types are different. However, both of your
examples show using the == operator on identical types. This is the way
I'm using the phrase "overload" as well.
It's not that the types are different to each other within the
signature - it's that the *signatures* are different.
Basically, if the string type didn't overload the == operator with
bool operator ==(string x, string y)
then there wouldn't be any such operator. There's be the implicit
bool operator ==(object x, object y)
but that's got a different signature, so isn't overridden by the string
one. (And wouldn't be anyway, as they're always static... which I
realise I haven't included above.)
Is there anything in the article which goes against that? I could see
anything in a brief skim, but I'm happy to look at any particular bits
more closely.
Look at the text in the section named "Overriding Operator ==":
By default, the operator == tests for reference equality by determining
if two references indicate the same object, so reference types do not
need to implement operator == in order to gain this functionality. When
a type is immutable, meaning the data contained in the instance cannot
be
changed, overloading operator == to compare value equality instead of
reference equality can be useful because, as immutable objects, they
can
be considered the same as long as they have the same value. Overriding
operator == in non-immutable types is not recommended.
Overloaded operator == implementations should not throw exceptions. Any
type that overloads operator == should also overload operator !=. For
example:
I do not read that text as talking about anything other than changing the
behavior of the == operator in a specific case. But within two
paragraphs, the word "override" is used only once (twice, if you count the
heading), while the word "overload" is used four times. As near as I can
tell, the words are used interchangeably.
"Overriding" there is a mistake. As operators are implemented as static
methods, they can't be overridden. Apologies for not finding it before
- I looked for "override" which of course misses "overriding".
I haven't been following the conversation closely enough, but overload
and override are pretty well defined in the C# and .NET world.
I certainly understand the distinction (even if I've confused the two on
occasion in the past :) ). However, I have only ever called the act of
changing an operator's beahvior "overloading", even when I haven't changed
the types involved in the comparison. Nor have I ever seen documentation
that makes a clear distinction between the two in that case.
There's no such thing as operator overriding, basically - could you
given an example where you think you *have* been doing what you'd call
operator overriding?
Even in the
context of C#, where the two terms are clearly distinct as applied to
methods, when talking about operators, the same care is not taken to
distinguish the two.
It is by some - just not by all :)
--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.
- Follow-Ups:
- Re: compare two structs via ==
- From: Peter Duniho
- Re: compare two structs via ==
- References:
- Re: compare two structs via ==
- From: Mark Wilden
- Re: compare two structs via ==
- From: Göran Andersson
- Re: compare two structs via ==
- From: Mark Wilden
- Re: compare two structs via ==
- From: Göran Andersson
- Re: compare two structs via ==
- From: Mark Wilden
- Re: compare two structs via ==
- From: Peter Duniho
- Re: compare two structs via ==
- From: Mark Wilden
- Re: compare two structs via ==
- From: Peter Duniho
- Re: compare two structs via ==
- From: Mark Wilden
- Re: compare two structs via ==
- From: Peter Duniho
- Re: compare two structs via ==
- From: Jon Skeet [C# MVP]
- Re: compare two structs via ==
- From: Peter Duniho
- Re: compare two structs via ==
- Prev by Date: Re: Wich DBMS supports TransactionScope and distributed transactions?
- Next by Date: Re: static class inheritance, generalized
- Previous by thread: Re: compare two structs via ==
- Next by thread: Re: compare two structs via ==
- Index(es):
Relevant Pages
|