Re: how to make two references to one string that stay refered to the same string reguardless of the changing value in the string?

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 11/03/04


Date: Wed, 3 Nov 2004 15:26:15 -0000

Zach <not@this.address> wrote:
> "Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
> news:MPG.1bf2f97a8da9b08b98b870@msnews.microsoft.com...
> > Zach <not@this.address> wrote:
> > > John - hopefully not a terribly dumb question - surely, if you want
> > > to save room and a value is likely to be repeated very often, you can
> > > use a code list, provided of course that the code value is less
> > > memory consuming than the value returned by the code list?
> >
> > I'm not entirely sure what you mean - could you clarify your question?
>
> I understood that the problem was that a certain value might be repeated
> many times in different relationships and that you were providing a medicine
> to keep memory usage down to a minimum.
>
> Thag is why I thouht, rather than filing Monty Ville, you could file 12,
> and have a conversion table to say that 12 translates into Monty Ville.
> This is the point I was attempting to make, and wondered whether
> I was missing the essence of what you were explaining. Obviously
> my example implies that there are lot of, albeit limited number of,
> names of towns.

Given that you've got to store "Monty Ville" somewhere, you might as
well just have several references to the same string. If you know
you'll only have 256 town names, you could store each value as a byte
index, of course, and likewise if you have only 65536 names you could
store each value as a ushort. It's typically not going to be worth
doing that though.

The code I gave will make sure that only one copy of the town name is
used (with other copies just being temporary). Every reference to
"Monty Ville" will be a reference to the same string object. References
are pretty cheap in memory - four bytes in the current CLR
implementation.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Quantcast