473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

String handling

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

Sep 12 '06 #1
2 1329
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Richard" <to**********@yahoo.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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

Sep 12 '06 #2
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

"Richard" wrote:
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

Sep 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lkrubner | last post by:
Last year I asked a bunch of questions about character encoding on this newsgroup. All the answers came down to using ord() in creative ways to try to make guesses about multi-byte characters. I...
4
by: Ken Fine | last post by:
No joy on Macromedia's boards after a few days; maybe someone can help me here. I got an excellent string handling function off of planet-source-code.com that converts text strings to proper...
4
by: Rajeev | last post by:
Hi I am a VB programmer. I need to do a simple thing in VC++. char strNWUserName; DWORD dBufferLength = 256; HRESULT hr; hr = WNetGetUserA("DOMAIN", strNWUserName, &dBufferLength); The...
1
by: Derek | last post by:
A common technique for trimming leading and trailing spaces from std::string is the following: string s(" blah blah blah "); const char* ws= " \t\r"; string::size_type not_white; // trim...
6
by: rakesh | last post by:
Hi, I have a requirement in which I need to search a string in a file stored on a harddisk. String which needs to be searched would be stored in a file with delimiter as new line character. some...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
11
by: Peter Kirk | last post by:
Hi i have a string variable which can take one of a set of many values. Depending on the value I need to take different (but related) actions. So I have a big if/else-if construct - but what is...
12
by: kevineller794 | last post by:
I want to make a split string function, but it's getting complicated. What I want to do is make a function with a String, BeginStr and an EndStr variable, and I want it to return it in a char...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.