Re: Why cast to Object to compare strings?

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

From: Bill Butler (Bill_at_DigitalArts.com)
Date: 03/23/05


Date: Wed, 23 Mar 2005 16:20:09 -0500


"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1cabda88f3df860c98bed5@msnews.microsoft.com...
> Bruce Wood <brucewood@canada.com> wrote:
...
>> I don't see the point of comparing strings character-by-character when
>> you know that all references to identical strings point to the same
>> memory location...?
>
> They don't. Try the following:
>
> using System;
>
> public class Test
> {
> static void Main()
> {
> string x = "x";
> string y = x+"y";
> string z = x+"y";
>
> Console.WriteLine (y==z);
> Console.WriteLine ((object)y==(object)z);
> }
> }

Thanks Jon,
   I guess I should have tested before posting.
   Do you happen to know the actual algorithm for string.Equals()?

   I played around a bit and found that it checks for Length equality before
resorting to brute force

 Bill



Relevant Pages

  • Re: averages
    ... That Bill has mental issues is apparent to everybody (it has been mentioned ... references to it in the usual usenet archives. ... point to 'short-term' memory problems. ... don't remember that you made a resolution, then you can't possibly keep it. ...
    (comp.lang.c)
  • Re: [patch 1/6] mmu_notifier: Core code
    ... external references to pages managed by the Linux kernel. ... access memory managed by the Linux kernel. ... The MMU notifier will notify the device driver that subscribes to such ...
    (Linux-Kernel)
  • Re: [PHP] Odd PHP memory issue
    ... all references to the result variable are unset when I ... to see if the memory is getting chewed up in a straight line or if it ... Scope seems like it should be simple, ... posting this question was to find out more about how PHP internals work, ...
    (php.general)
  • Re: Writing huge Sets() to disk
    ... that this code takes a lot of extra memory. ... > I believe it's the references problem, ... It's a bit unfortunate that all those instance variables are global to ... it merely aggregates it for use in storing new Python objects. ...
    (comp.lang.python)
  • Re: How to track memory usage?
    ... the IE memory leak issue is real, ... And JScript in IE allows the garbage collector to be explicitly ... but then removes all references to that object ... "Break Circular References" - examines all DIVs and removes ...
    (comp.lang.javascript)