I need some clarification over the below code. I know that str.Length will return the number of characters in the string.
string str = "Sample string";
int length = str.Length;
My clarification is: Since we are not creating the string object, how the “Sample string” string is assigned to str variable?
The string literal
"Sample string"is created by the compiler, and will be stored in the assembly for you. When you assign it to your reference, you get a reference to that literal string.There is a
ldstrinstruction that specifically loads literal strings from assembly metadata into astringobject reference. It is that reference that has it’sLengthproperty checked.