String handling

  • Thread starter Thread starter Richard
  • Start date Start date
R

Richard

Hi,
I'm wondering if anyone can answer these questions for me (or point me
to somewhere maybe on msdn which covers this if it exists)

Take the following code:

switch (aValue)
{
case "Something":
case "SomethingElse"
}

In this code, would there be the equivalent of two string objects
created in memory? Or does C# do something clever here?

With the same example above, does C# have a symbol table in the
compiled output where these get stored? I know its considered good
practise if say the string "Something" is used frequently to put it
into a constant, but I recall back in VB5 days that it was actually
faster to put it in constants as well. I wondered if the same applies
for C#.


Many thanks,
Richard
 
Richard,

If you have two string literals that are the same, they will appear once
in the string table for the module that the code is compiled in.

The biggest benefit for placing the string in a constant is that if you
have it used in multiple places, you can change that value easily if you
need to.

Hope this helps.
 
I good thing to search for on this is string interning or String.Intern(string)
The way the guys (and gals) at MS did this for switch statements in
particular is very clever. A book that describes it is "Applied .NET
Framework Programming" from MS Press.

HTH


Ciaran O'Donnell
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top