Re: Explanation of object equality.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: how to make two references to one string that stay refered to the same string reguardless of the
    ... > You can use the string interning pool. ... > VehicleYear ... String interning is considerably slower than simple assignment. ... same kind of thread safety that the intern pool does. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how to pass by const reference?
    ... Oh, and I forgot to mention, technically the interning of constant strings is not done by the compiler, rather it is done by the JIT/runtime. ... You can turn off string interning of constants by ngen'ing the assembly when you have a no interning attribute in the assembly. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Huge string arrays in C#
    ... > technique of string interning. ... > have one copy in memory of each unique string value. ... > haven't seen a noticeable performance penalty for access once all the ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Huge string arrays in C#
    ... > processing scenarios where the program will exit when done with the strings. ... I assumed interning would have ... I can understand that with string constants, ... I *think* it's done on an AppDomain basis rather than a process basis, ...
    (microsoft.public.dotnet.framework.performance)
  • Re: interning strings
    ... > String constants that are potential attribute names are also interned: ... This interning is done in the compiler, ...
    (comp.lang.python)