9

By default I use string constants in my code when I have a string of text that will be output to the screen via a messagebox, label, etc. My main reason for doing so is that I do not want these strings to change at runtime and this way nobody really touches them.

My question is:

It seems that from a memory point of view, this approach is creating an object whose lifetime will be decided by the GB whereas a string literal seems more efficient and will be disposed of more quickly. I know this is a rookie question but is there a benefit (no matter how tiny) when using a literal over a constant or should a constant be used in such scenarios.

I know it will not make any noticeable difference in my apps but I am trying to improve myself as a dev and build standards that I keep a reasonable amount of the time. :o)

2
  • Maybe my English isn't good enough but what is the difference between a "string constant" and a "string literal". Aren't they the same? Commented Jul 22, 2009 at 15:53
  • 2
    a string literal is the part between the quotes ("foo"). A constant is when you assign a literal to a name (const string FOO = "foo"). I think when Jason says literal he means using the literal inline (MessageBox.Show("foo")). Commented Jul 22, 2009 at 15:58

5 Answers 5

15

Neither. The recommended way AFAIK is to use a resources file.

Sign up to request clarification or add additional context in comments.

2 Comments

I agree. Forget literals and constants. Definitely use a resource file. That way the strings are kept externally and are language independent.
Depending, of course, on the context. No need to SOFT_CODE: en.wikipedia.org/wiki/Softcoding =)
5

Prefer string constants to string literals, but the reason is to make your code more maintainable, not to improve its performance.

3 Comments

Amen. Maintainability will pay off ten times more than a micro-optimization like the one proposed by the OP.
@BilltheLizard How is using string constants more maintanable than string literals?
@MehdiCharife The same way using any constant is more maintainable than using literal values. The more copies of the same literal value you have in your code, the more places you have to change it when the value needs to change. If you define a constant instead, you only have to change it in one place.
3

You should look into "string interning". Basically, all of the unique string literals (constant or inline) in any of your source files will be stored in a special table. They will never be garbage collected, and may benefit from re-use.

You might want to look into using a resources file if you will ever be changing those strings without wanting to recompile (eg for internationalisation)

2 Comments

the upshot of string interning is that you should do whatever is most maintainable
I believe string interning is a default in C# in 4.0. I think it's also the case that the compiler inserts literals into the code in the place of constants (and enums). This means that separating the constant from it's implementation can cause versioning issues. Agree with Rob above that you should do what's most maintainable- but don't limit that statement to string interning.
2

For internationalization purposes, it's true that the accepted best practice/recommendation is a set of resource files.

I am following the same pattern of using string constants in their own class. I find it's easier to create consts that'll be used on multiple pages throughout the web app. Even a const with placeholders helps!

  //{1} for the user's full name for example
const string SOME_LABEL = "Thanks for purchasing {0} products, {1}!";

Comments

1

It would depend on whether you need these strings to be localizable or not. If you need the ability to translate your software into other languages, then I would place as many strings that are displayed to an end user as you can in resource files. If you do not need localization, or if the strings in question are never displayed to an end user, I would use constants. If your strings are shared amongst multiple classes, I would create classes that wrap up common strings that are provided as public constants. Unless these strings need to be used outside of the assembly they are defined in, keep them at the lowest privilege level possible (private, protected, internal).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.