Why don’t we use new operator while initializing a string?

Strings are immutable reference types. There’s the ldstr IL instruction which allows pushing a new object reference to a string literal. So when you write:

string a = "abc";

The compiler tests if the "abc" literal has already been defined in the metadata and if not declare it. Then it translates this code into the following IL instruction:

ldstr "abc"

Which basically makes the a local variable point to the string literal defined in the metadata.

So I would say that your answer is not quite right as the compiler doesn’t translate this into a call to a constructor.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)