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

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

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

unit testing - How to mock PreferenceManager in Android? -