Strings Are A Reference Type

Strings Are A Reference Type

It doesn’t always seem so, but the .Net String type in VB really is a reference type.

The reason that it doesn’t always seem so is that .Net has some behind-the-scenes trickery to make it seem like strings are a value type.  These tricks enable the programmer to (mostly) use strings in the same way that integer, single or double types are used, which has historically been what VB programmers are used to.

1. Strings look like a value type because they do not require a constructor.   You can write
    Dim Str As String = “1234”
instead of
    Dim Str As String = New String(“1234”)

This is a special feature of the string assignment operator (“=”).  The assignment operator accepts a string literal, creates a new string from the literal, and assigns it to the string variable.  

2. Strings look like a value type because they appear to have a default initialisation.   You can write
    Dim Str As String
    Str &= “1234”

The expression evaluator automatically treats a string which is nothing as if it were an empty string, so it appears as if the string was created as an empty string, even though it was actually created as Nothing (as for any object).  You may still get a warning about using a variable before it has been assigned a value, but there is no run-time error as would usually occur if a reference variable was used before it had been assigned to a new instance of the object.   

3. Strings look like a value type because you can assign an expression to the variable.  You can write
    Dim Str1 As String = “”
    Dim Str2 As String = “12345”
    …
    Str1 = Str2.SubString(3)

This is a special feature of the string assignment operator (“=”).  The assignment operator accepts a string expression, creates a new string object from the expression, and assigns it to the string variable. A string method (such as SubString) is a string expression that works the same way.

4. Strings look like a value type because you can use them without nominating a property or method.  You can write
    Dim Str1 As String = “”
    Dim Str2 As String = “12345”
    …
    Str1 = “ABC” & Str2
    ‘ Instead of
    ‘ Str1.StringValue = “ABC” & Str2.StringValue   

This is a special feature of the expression evaluator.  A variable that refers to a string is always evaluated as the default ‘string value’ property of a string.  In fact, there is no property of the class that returns the ‘value’ – it is only directly accessible using the instance variable itself.

5. Strings look like a value type because string comparisons are value comparisons, not reference comparisons.

This is a special feature of the Compare and Equals methods of the string class.  Object comparisons are usually reference comparisons.  That is, if two variables that refer to object types are compared, the comparison is determined by whether they are the same object or not, and concepts of ‘less than’ or ‘greater than’ do not exist.
    If Button1 = Button2 …
returns True only if the variable Button1 refers to the same object that the variable Button2 refers to. Similarly,
    If Button1 <= Button2
is meaningless because there is no concept of order between buttons.  However,
    If Str1 = Str2 Then …
is a comparison based on the values of the string, and
    If Str1 < Str2 Then …
is a valid comparison, because there are rules for determing what ‘<‘ and ‘>’ mean for the values of the string objects.

Note that the rules for comparing strings can be very complex, but the point here is that the comparison is based on values, just like value types are always compared by their values.

Note also that there is another feature of strings that affects comparison. String values are stored in a special table, so that identical string values are not duplicated.  Two string objects that have the same value are stored as one entry in the table, and the two string variables refer to that one entry.  Therefore the Is operator, which is used to check if two object references are the same, may return True for two strings that have the same value, even if the variable values have been set quite separately from each other. (May has been used above because there are some compiler options that affect the way that the string literal table is handled, and therefore affect the Is operator).

There is one single feature that proves that strings really are a reference type –

1. A string can be nothing.  A value of Nothing means that the variable does not refer to an object, so only a reference type can have a value of Nothing. (A string can also be empty, which means there is an object for the variable to refer to, but the object has no characters in it).  If a string variable can be Nothing then the variable type must be a reference type.

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s