Strings Are Immutable

One of the interesting aspects of System.String is that once you assign a String object with its initial value, the character data cannot be changed. At first glance, this might seem like a flat-out lie, given that we are always reassigning strings to new values and due to the fact that the System. String type defines a number of methods that appear to modify the character data in one way or another (uppercase, lowercase, etc.). However, if you look closer at what is happening behind the scenes, you will notice the methods of the String type are in fact returning you a brand-new String object in a modified format:

Module Program Sub Main()

Console.WriteLine("***** Fun with Strings *****") ' Set initial string value.

Dim initialString As String = "This is my string." Console.WriteLine("Initial value: {0}", initialString)

' Uppercase the initialString?

Dim upperString As String = initialString.ToUpper() Console.WriteLine("Upper case copy: {0}", upperString)

' initialString is in the same format! Console.WriteLine("Initial value: {0}", initialString) End Sub End Module

If you examine the relevant output in Figure 3-9, you can verify that the original String object (initialString) is not uppercased when calling ToUpper(), rather you are returned a copy of the string in a modified format.

Figure 3-9. Strings are immutable!

The same law of immutability holds true when you use the VB 2008 assignment operator. To illustrate, comment out any existing code within Main() (to decrease the amount of generated CIL code) and add the following code statements:

Module Program Sub Main()

Dim strObjA As String = "String A reporting." strObjA = "This is a new string" End Sub End Module

Now, compile your application and load the assembly into ildasm.exe (again, see Chapter 1). If you were to double-click the Main() method, you would find the CIL code shown in Figure 3-10.

Figure 3-10. Assigning a value to a String object results in a new String object.

Although I don't imagine you are too interested in the low-level details of the CIL, do note that the Main() method makes numerous calls to the ldstr (load string) opcode. Simply put, the ldstr opcode of CIL creates a new String object. The previous String object that contained the value "String A reporting." is no longer being used by the program, and will eventually be garbage collected.

So, what exactly are we to gather from this insight? In a nutshell, the String type can be inefficient and result in bloated code if misused. If you need to represent basic character data such as a US Social Security number, first or last names, or simple string literals used within your application, the String data type is the perfect choice.

However, if you are building an application that makes heavy use of textual data (such as a word processing program), it would be a very bad idea to represent the word processing data using String types, as you will most certainly (and often indirectly) end up making unnecessary copies of string data. So what is a programmer to do? Glad you asked.

Was this article helpful?

0 0

Post a comment