c# - Why don't we use new operator while initializing a string? -


i asked question in interview: string reference type or value type.

i said reference type. asked me why don't use new operator while initializing string ? said because c# language has simpler syntax creating string , compiler automatically converts code call construcor of system.string class.

is answer correct or not ?

strings immutable reference types. there's ldstr il instruction allows pushing new object reference string literal. when write:

string = "abc"; 

the compiler tests if "abc" literal has been defined in metadata , if not declare it. translates code following il instruction:

ldstr "abc" 

which makes a local variable point string literal defined in metadata.

so answer not quite right compiler doesn't translate call constructor.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -