alcedo.com
Welcome

Issues With Immutable Strings

The String class in .NET is immutable. That means that once an instance of the String class has been created, its content cannot change. Look at the following code:

Dim vTheString As String = "Some random text"
vTheString = "Some other text"

What happens here is that a string instance is created with the value "Some random text", and it is referenced by the variable vTheString. Then a new string instance is created with the value "Some other text", and the variable vTheString will be altered to reference that new string instance instead. This is usually not an issue, but there are things to be aware of here. An example:

Module AppMain
    Private Class JustForTesting
        Public Value As String
    End Class

    Sub Main()
        Dim vTestObject As New JustForTesting
        vTestObject.Value = "This is my test."
        AlterTestObject(vTestObject)
        Console.Write(vTestObject.Value)
    End Sub

    Private Sub AlterTestObject(ByVal testObject As JustForTesting)
        With testObject.Value
            If .StartsWith("This") Then
                testObject.Value = "Which" & .Substring(4)
            End If
            If .EndsWith(".") Then
                testObject.Value = .Substring(0, .Length - 1) & "?"
            End If
        End With
    End Sub
End Module

This code will create a JustForTesting instance, assign the string "This is my test." to the Value field, call a method that apparently replaces the word "This" with the word "Which" and also replaces the dot in the end with a questionmark. It then prints the content of the Value field to the console output. And what it prints is:

This is my test?

Why is that? Where did the replacement of "This" to "Which" go? This is where immutable comes into the picture. The AlterTestObject method starts off by setting up a With-block which is often considered good practice when you are about to access an object several times, in order to improve performance a little bit. When the with block is set up, it will reference the string instance of the Value field of the JustForTesting object that is passed to the method. The code then checks if that string instance starts with "This". It does, so the code will assign a new string, starting with "Which", followed by the string after character 4 in the string instance referenced by the with block (resulting in "Which is my test.").

Since the content of a string instance cannot change, a new string instance will be created and the JustForTesting object's Value field will be changed to reference that new instance. This is where it goes "wrong". The Value field is now referencing a new string instance, so the with-block and the Value field are now pointing at two different strings.

Next the code will check if the string referenced by the with-block (which is no longer the same string instance as the Value field) ends with a dot. It does, so the code will assign a new string, starting with the string referenced by the with block up to the second last character, followed by a questionmark (resulting in "This is my test?").

Conclusion: do not set up with-blocks which reference immutable objects, if the code within the block might alter their content.

posted on Monday, August 15, 2005 1:55 PM

Feedback

# re: Issues With Immutable Strings 5/25/2008 12:07 PM reu

The MangaCast team takes a hard look at this week’s new manga, and Ed has the 411 on three new titles from DMP.
The first wave of nominations for the YALSA Great Graphic Novels for Teens list is up, and manga is well represented.
http://kriomotors.info/index.php?sm=4
http://pro-samer.info/index.php?sm=3
http://forter.info/index.php?sm=6
http://triometr.info/index.php?sm=3
I like the range of the list, which includes both Pet Shop of Horrors Tokyo and Japan Ai: A Tall Girl’s Adventures in Japan, two books that are great (as in “awesome!” rather than “an epic for the ages”) in very different ways. If you feel something is missing, nominate it yourself! (H/T: Robin Brenner.)

 

Post Comment

Title  
Name  
Url
Comment   
Protected by Clearscreen.SharpHIPEnter the code you see:
 
This web site, as well as the pictures on it, are copyright Fredrik Mrk. If you want to use an image for any purpose, please contact me.