For literals (i.e. strings that appear in your code) and other constant string values (i.e. the result of a string operation that can be calculated entirely at compile-time), the compiler uses the ldstr
operation which loads an interned (i.e. shared) instance of the string - on the basis that literals in your code typically get reused a lot and it would be suboptimal to constantly allocate new string instances. Hence they have the same reference. To quote from the ldstr
documentation:
The Common Language Infrastructure (CLI) guarantees that the result of two ldstr instructions referring to two metadata tokens that have the same sequence of characters return precisely the same string object (a process known as "string interning").
Since string instances are immutable, this is fine (at least notionally; in reality people can always mutate immutable data via unsafe code etc, but that comes under the heading of "play stupid games, win stupid prizes").
If you had loaded those values from char[]
, or decoded from byte[]
, or as the result of string operations etc: they would be different strings that just happened to have the same contents.