Re: Why cast to Object to compare strings?
From: Bill Butler (Bill_at_DigitalArts.com)
Date: 03/23/05
- Next message: Peter N Roth: "Re: is there a C# equ. of the Delphis With x do?"
- Previous message: Jon Skeet [C# MVP]: "Re: constructor chaining vs calling this. vs calling named constructor?"
- In reply to: Jon Skeet [C# MVP]: "Re: Why cast to Object to compare strings?"
- Next in thread: Jon Skeet [C# MVP]: "Re: Why cast to Object to compare strings?"
- Reply: Jon Skeet [C# MVP]: "Re: Why cast to Object to compare strings?"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Peter N Roth: "Re: is there a C# equ. of the Delphis With x do?"
- Previous message: Jon Skeet [C# MVP]: "Re: constructor chaining vs calling this. vs calling named constructor?"
- In reply to: Jon Skeet [C# MVP]: "Re: Why cast to Object to compare strings?"
- Next in thread: Jon Skeet [C# MVP]: "Re: Why cast to Object to compare strings?"
- Reply: Jon Skeet [C# MVP]: "Re: Why cast to Object to compare strings?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|