Jon,[color=blue]
> However, just to talk about interning for a second - there's a problem
> with this, which is that the intern pool won't be garbage collected
> until the app domain is (IIRC).
>
> A simpler solution is to just use a hashtable to do the same kind of
> thing:[/color]
Or an XmlNameTable if you are working with XML.
Hope this helps
Jay
"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1bf2a3ce34034e3898b86b@msnews.microsoft.c om...[color=blue]
> Bob Grommes <bob@bobgrommes.com> wrote:[color=green]
>> You can use the string interning pool. All string constants are interned
>> at
>> compile-time by default but you have runtime access to this facility if
>> you
>> want to put other strings in there:
>>
>> string s1 = String.Intern("foo");
>> string s2 = String.Intern("foo");
>>
>> Both references point to the same "foo" in memory, rather than to two
>> different "foo"s. If one of them is reassigned later, that is properly
>> managed:
>>
>> s2 = String.Intern("bar");
>> string s3 = String.Intern("foo");
>>
>> Now s1 & s3 point to the same "foo", s2 points to bar.
>>
>> This can be a tremendous memory saver. For example if you load 1,000
>> rows
>> from, say, a comma-delimited file into memory, with these fields:
>>
>> Name
>> Address
>> City
>> State
>> Zip
>> VehicleYear
>> VehicleMake
>> VehicleModel
>>
>> You could intern the last 6 fields and possibly save 90% or more of the
>> memory consumption for those values since there are likely to be
>> relatively
>> few unique values in those fields.
>>
>> Warning: String interning is considerably slower than simple assignment.
>> I
>> haven't formally benchmarked it but I think the penalty is fairly stiff.[/color]
>
> I think this is actually looking at the opposite problem from what the
> OP is after. I believe he wants to do:
>
> string s1 = "hello";
> string s2 = s1;
>
> s1 = "bar";
> // I think the OP wants s2 to be bar here
>
> However, just to talk about interning for a second - there's a problem
> with this, which is that the intern pool won't be garbage collected
> until the app domain is (IIRC).
>
> A simpler solution is to just use a hashtable to do the same kind of
> thing:
>
> Hashtable table = new Hashtable();
> string PseudoIntern (string x)
> {
> string ret = table[x] as string;
> if (ret != null)
> {
> return ret;
> }
> else
> {
> table[x] = x;
> return x;
> }
> }
>
> This may well be faster than interning, too, as it doesn't require the
> same kind of thread safety that the intern pool does. (Depending on
> your usage pattern, of course...)
>
> --
> Jon Skeet - <skeet@pobox.com>
>
http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]