CHAPTER 3 CORE C# PROGRAMMING CONSTRUCTS, PART I
.locals init ([0] string s2)
IL_0000: nop
IL_0001: ldstr
"My other string"
IL_0006: stloc.0
IL_0007: ldstr
"New string value"
IL_000c: stloc.0
IL_000d: ret
} // end of method Program::StringAreImmutable2
Although we have yet to examine the low-level details of the CIL, note the numerous calls to the
ldstr (load string) opcode. Simply put, the ldstr opcode of the CIL loads a new string object on the
managed heap. The previous string object that contained the value "My other string" will eventually
be garbage collected.
So, what exactly are we to gather from this insight? In a nutshell, the string class can be inefficient
and result in bloated code if misused, especially when performing string concatenation. If you need to
represent basic character data such as a U.S. Social Security number, first or last names, or simple bits of
text used within your application, the string class is the perfect choice.
However, if you are building an application that makes heavy use of frequently changing textual
data (such as a word processing program), it would be a very bad idea to represent the word processing