10

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!

O.O
  • 11,077
  • 18
  • 94
  • 182

9 Answers9

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.

Guilherme Duarte
  • 3,371
  • 1
  • 28
  • 35
  • 5
    Note that of course "constant variable" is an oxymoron. A constant by definition is not a variable. – Eric Lippert May 04 '11 at 21:45
  • 3
    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". – Michael Stum May 04 '11 at 22:43
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.

Florian Greinacher
  • 14,478
  • 1
  • 35
  • 53
  • 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`? – Andrii Jul 16 '18 at 15:42
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?

Jon
  • 969
  • 4
  • 9
  • 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 Jul 16 '18 at 15:43
  • @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. – Jon Jul 24 '18 at 22:32
  • @Andrii try this one: https://www.c-sharpcorner.com/UploadFile/d551d3/what-is-string-interning/ – Jon Jul 28 '18 at 01:46
  • Cool article. I found some new thing. But it does not answers to my question – Andrii Jul 30 '18 at 07:53
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.

Lohith S
  • 89
  • 3
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.

Oren A
  • 5,870
  • 6
  • 43
  • 64
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.

joce
  • 9,624
  • 19
  • 56
  • 74
  • @gjsduarte - I removed subtitle and replaced it with title. These guys and gals are fast! – O.O May 04 '11 at 20:28
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

Ahmed
  • 11,063
  • 16
  • 55
  • 67
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.

Khepri
  • 9,547
  • 5
  • 45
  • 61
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...

Ivan Crojach Karačić
  • 1,911
  • 2
  • 24
  • 44