Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between string and const string

Tags:

c#

refactoring

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!

like image 294
O.O Avatar asked May 04 '11 20:05

O.O


1 Answers

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.

like image 53
Guilherme Duarte Avatar answered Oct 20 '22 15:10

Guilherme Duarte