The difference between string and string const

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!

+6
source share
9 answers

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.

+4
source

, , , . , 1000 , , , 1000 , . , . , . readonly .

+3

, . const, ( ) const IL; .

, , . , ... , . const, .

const string title = ... const , .

, , .

const?

+2

, , , - ( ), , var .
, , , , . . , , .

+1

title, const .

, :

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

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

.

+1

(.. ), , . . , , , , , , .

2

+1

, , , , , .

0

, , , , refacotring...

0
source

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.

0
source

All Articles