The refactoring tool I use often assumes that I have changed something:
string title = "Some title.";
to
const string title = "Some title.";
Why, what is / - difference (s)?
Thank!
const is the prefix of a constant variable. One that does not change at runtime.
Usually, if you have a variable that matches this, you should declare it a constant (const) to avoid code errors and enable compilation of optimizations.
This is why the refactoring tool does this for you.
, , , . , 1000 , , , 1000 , . , . , . readonly .
, . const, ( ) const IL; .
, , . , ... , . const, .
const
const string title = ... const , .
const string title = ...
, , .
const?
, , , - ( ), , var ., , , , . . , , .
title, const .
title
, :
string title = "123"; title = "fool";
const string title = "123"; title = "foo"; // NO!
.
(.. ), , . . , , , , , , .2
, , , , , .
, , , , refacotring...
const is compilation time. When you use a constant string, you are not using a space for this variable at runtime. The compiler uses this value in a way that is not like a macro. When you do not use a constant string, it acts like any other variable and takes up additional space at runtime.