C # - Is this declared string declared as const?

Help me resolve the dispute here.

It:

SqlCommand cmd = new SqlCommand( "sql cmd", conn);

handled exactly the same as this:

const string s = "sql cmd";
SqlCommand cmd = new SqlCommand( s, conn);

Those. it matters if I specifically declare that string s is a constant.

And if it is not treated equally, why not?

+5
source share
6 answers

In the last fragment, this does not mean that the string const is a const variable. This is not exactly the same as const in C ++. (Strings are always immutable in .NET.)

, . , s, ​​ ( ), . , - , "sql cmd" , ... , , , , PDB, .

+6

a const , , .

, , - a const, , ( - ).

+2

SqlCommand "" - .

+1

, .

+1

, Reflector, , . , :)

+1

100%, , .

const , , , .

, , , , .

However, some evidence will study the IL code generated in both cases.

0
source

All Articles