11

The refactor tool I'm using often suggests that I change something like this:

string title = "Some title.";

to

const string title = "Some title.";

Why, what is/are the difference(s)?

Thanks!

3
  • 1
    Did it really suggest renaming it as well? Commented May 4, 2011 at 20:23
  • No, that was my mistake. Commented May 4, 2011 at 20:30
  • Shameless self plug: stum.de/2009/01/14/… shows how a const string gets compiled. Commented May 4, 2011 at 22:44

9 Answers 9

6

const is the prefix of a constant variable. One that doesn't change at runtime.

Usually if you have a variable that meets this you should declare it as constant (const), both to avoid mistakes in the code and to enable compiling optimizations.

This is why the refactoring tool does it for you.

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

2 Comments

Note that of course "constant variable" is an oxymoron. A constant by definition is not a variable.
It's more than a variable that doesn't change at runtime - it's a literal expression. It's not a reference to anything anymore, it's essentially the compiler copy/pasting the actual constant value into all the "call sites".
3

Well, in my opinion the major point in using constant strings is that a constant string is automatically interned. So if you have 1000 instances of a type that has a regular string field and all instances store the same string that will never be changed then 1000 equal string instances will be stored, unnecessarily blowing up the memory profile of your application. If you declare the string constant it will only consume memory once. This is the same behavior as using the string literal directly. In contrast to a static readonly string the value of constant string is stored directly in the referencing class.

1 Comment

Could you clarify? Does your explanation mean that const string with producing a single instance of the string per application domain for all occurrences the same as static readonly string?
2

Along with what was said by the others. When you declare a local variable with const, the compiler (in release mode) will replace usages of the variable with the const value in IL; resulting in a smaller stack.

Strings specifically are a special case. During compilation, the compiler runs through a string-interning process where the string variable you created may actually point to existing string, or a new one... since strings are immutable, it doesn't usually matter too much. This is not specific to const strings, but rather string literals.

In the example case of const string title = ..., const means that the value is assigned at the time of declaration and it cannot be changed.

This is a related question that may have what you're looking for.

Is there a runtime benefit to using const local variables?

4 Comments

But string is not a value type it won't come to stack. Do you mean a size of address to that string instance?
@Andrii The "stack" comment is accurate enough for non-strings, strings are a special case. Take a look into how the c# compiler does string-interning.
Cool article. I found some new thing. But it does not answers to my question
1

To explicitly show that it doesn't change, so that when someone reads your code (including yourself) she'll know that this var doesn't change.
The tool probably suggests that for other types of vars as well, if you don't change them later. This is good practice.
Of course if you'll try to change it later, you'll get a compilation error.

Comments

1

In your example, the non-const title can be modified but the const one can't.

For example, you could do:

string title = "123";
title = "fool";

but not

const string title = "123";
title = "foo"; // NO!

It would not compile.

1 Comment

@gjsduarte - I removed subtitle and replaced it with title. These guys and gals are fast!
1

If you aren't going to change the value of your string (i.e. it will remain constant) then, it's better to Expressively indicate that it's a constant -That should help on the readability side. Technically speaking, the only difference I know differs to constants than non-constants is that all constants are static.
just my 2 cents

Comments

1

const is compile time. When you use const string you end up not using space for this variable during run time. The compiler uses this value in a way not dissimilar to a macro. When you don't use const string it acts like any other variable and occupies additional space during run time.

Comments

0

All the tool is suggesting is that based on it's analysis the string in question never changes so you might as well make it a constant so that it cannot be changed in the future.

Comments

0

seems just like you dont change title anywhere in the rest of the code so it's not a variable but a constant and that's what the refacotring tool suggests...

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.