Re: Explanation of object equality.
- From: "Jon Skeet [C# MVP]" <skeet@xxxxxxxxx>
- Date: Mon, 15 Sep 2008 23:29:37 -0700 (PDT)
On Sep 16, 6:59 am, DamienS <damiensaw...@xxxxxxxxxxxx> wrote:
G'day everyone and thanks for all your posts. My apologies for the
delayed reply.
Damien... Did we answer your question? You asked:
Look, to tell you the truth, I was thouroughly confused until your
explanation Jeff of interned strings. I've never heard of them. It's
far from it being "more than I wanted to know" - in fact, I guess
that's exactly the answer I was looking for when I posted the
question; namely an explanation of what I saw was an apparent
inconsistency. So - thanks for that :-)
For what it's worth, I do think that there's a language inconsitency
in that, for the following, Result=true:
object o1 = "hello";
object o2 = "hello";
bool Result = o1.Equals(o2);
Whilst Interning explains it perfectly
No, interning isn't really relevant in the above. The above will call
the *overridden* method in string.
Interning is only relevant when you're using ==, where the compiler
just uses the normal object definition, i.e. reference equality. This
is the big difference between overriding and overloading - overriding
is an execution time decision based on the actual type of the object.
Overloading is a compile time decision based on the declared types of
the arguments/operands.
I don't necessary agree with
the design of the language in permitting it, as it introduces
inconsistent behaviour. Surely the "interning engine" in the compiler
could determine that the objects were allocated differently and were
possibly intended by the devloper to indeed 'be' different, whilst
still maintaining the memory efficiency behind the scenes.
How could it determine that the objects were allocated "differently"?
They're allocated in the same way in two different lines, that's all.
I don't see any problem with string interning being in the language -
so long as it's well documented (which it is in the C# spec).
Jon
.
- References:
- Explanation of object equality.
- From: DamienS
- Re: Explanation of object equality.
- From: Jeff Louie
- Re: Explanation of object equality.
- From: DamienS
- Explanation of object equality.
- Prev by Date: Re: convert string to byte array
- Next by Date: Re: Spanish Characters in SQL File
- Previous by thread: Re: Explanation of object equality.
- Next by thread: RE: Form rendering issue
- Index(es):
Relevant Pages
|