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!
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!
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.
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.
const string with producing a single instance of the string per application domain for all occurrences the same as static readonly string?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.
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.
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.
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