473,326 Members | 2,133 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,326 software developers and data experts.

Question Regarding Const Strings Versus String Pooling

DLN
Hello all,

I have a quick question regarding how best to use static strings in my C#
code that I'm hoping someone can help me with. Is there any
advantage/disadvantage from a performance standpoint to declaring all my
string constants up-front as opposed to declaring them in-line? For
example:

public class ProjectConsts
{
public const String ConstOne = "This is a test";
public const String ConstTwo = "This is also a test";
}

And then referencing them in my code:

public void SomeMethod()
{
Console.WriteLine(ProjectConsts.ConstOne);
Console.WriteLine(ProjectConsts.ConstTwo);
}

public void SomeMethod2()
{
Console.WriteLine(ProjectConsts.ConstTwo);
Console.WriteLine(ProjectConsts.ConstOne);
}

Versus just using the string literals in-line such as:

public void SomeMethod()
{
Console.WriteLine("This is a test");
Console.WriteLine("This is also a test");
}

public void SomeMethod2()
{
Console.WriteLine("This is also a test");
Console.WriteLine(This is a test");
}

My understanding of string pooling (which may be incorrect) is that in the
second form, there will only be two string references and these references
will be reused in both method calls. I find that if I declare the strings
(as well as other intrinsic types) as constants in their own class and/or
assembly, my code is easier to read and maintain, but do I incur any sort of
performance penalty by declaring the types as const and using the constant
references in place of in-line strings?

Thanks.
Mar 6 '07 #1
1 1257
>but do I incur any sort of
performance penalty by declaring the types as const and using the constant
references in place of in-line strings?
No you don't.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 6 '07 #2

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

Similar topics

1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
2
by: djinni | last post by:
howdy, Can anyone tell me where the function name string is stored when __func__ is used? I read that it is basically like declaring a static const char, but when I compile the following code,...
5
by: Gelios | last post by:
Hello All! I am going to crazy and feeling myself so stupid but I don't understand such behaviour. I have code: public int getNextAgentId() { Int32 agent_id = 0; IDataReader dr =...
7
by: Mrinal Kamboj | last post by:
Hi , I am using OracleConnection object from Oracle ODP.net provider and following is the behaviour which i am finding bit strange : To start with my argument is based on followings facts : ...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
8
by: Ook | last post by:
I have a function getStuff, and two choices of implementation: const string *getStuff() { return &_stuff; } or const string getStuff()
11
by: copx | last post by:
Unforuntately, I know next to nothing about ASM and compiler construction, and while I was aware of the syntactic differences between pointers and arrays, I was not aware of this: ...
2
by: gdarian216 | last post by:
I am writting a program that has three different files and is compiled by a Makefile. The goal is to take a file of text and split it up in different sections and stored in vectors. Then it outputs...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.