2

I am reading the .NET IL Assembler book. There is a quote of the book:

`ldstr` "Enter a number" is an instruction that creates a string object from the specified string constant and loads a reference to this object onto the stack. The string constant in this case is stored in the metadata. You can refer to such strings as common language runtime string constants or metadata string constants. You can store and handle the string constants in another way, as explained in a few moments, but ldstr deals exclusively with common language runtime string constants, which are always stored in Unicode (UTF-16) format.

I see author writes that it is possible to refer to the string either as common language runtime string constant or as metadata string constant. While I do not understand the difference between these options, but my question is about something else...

Also, author writes twice about the using of the string by the ldstr instruction and everytime it is other (I selected it by the bold font).

So which type of the constant does the team use? Is it common language runtime string constant? Or is it metadata string constants?

Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
  • 1
    Pretty hard to guess what he was trying to say, we don't get the benefit of getting it "explained in a few moments". Always keep Ecma-335 handy whenever you do anything with IL. It only documents "string literal metadata token" as a valid operand to ldstr. Presumably he uses a const as equivalent to a literal. It is not, the compiler eliminates consts in the final IL. – Hans Passant Apr 14 '18 at 16:27
  • 1
    It looks to me like the author is offering *two different names* for the *same* concept. Thus there is *no* difference between the two since they're not *distinct* options. – Damien_The_Unbeliever Apr 17 '18 at 06:53
  • @Damien_The_Unbeliever: I can only assume that they are using the term "metadata string constants" to refer to strings in the `#Strings` stream (UTF-8, used to store identifiers like class/member names) and "common language runtime string" to refer to strings in the `#US` stream (UTF-16, used to store strings loaded by `ldstr`). – Brian Reichle Apr 22 '18 at 04:22
  • @BrianReichle - I'm just trying to make sense of the fragment "such strings". It's clearly a backreference. But any previous "string" reference within the quote seems to be to a single concept. And then the later part of the "such strings" sentence introduces two names. Either there's more context required, not included in the quote in the question, or, as I've suggested, it's two names for the same concept. What is your reading of "such strings" that means that we now need two separate names? – Damien_The_Unbeliever Apr 22 '18 at 16:38

1 Answers1

0

I've done some research on this for the last couple of days and came up with a possible answer.

The book .NET 2.0 IL Assembler seems to use the same Example as your book. They talk about that the instruction

ldstr "Enter a number"

loads the constant "Enter a Number" into the metadata. Same as your example it states that it can be called either common language runtime string constants or metadata string constants, it seems to make no difference in this case. As for my understanding it just plain says that the string constant is formatted in UTF-16. If you'd want to use strings in ANSI formatting, you'd have to use placeholders. (Which I can't explain since I'm new to the topic too).

So my conclusion for this problem was that it simply comes down to the UTF-16 encoding of the metadata.

Vulpex
  • 1,041
  • 8
  • 19