views:

1279

answers:

5

I know strings are immutable, so the minute you change a string reference's value .NET makes a brand new string on the heap.

But what if you don't change the value of a string reference; rather, you simply pass it into a function ByVal -- does this operation copy the string value on the heap as well? My inclination is "no," but I'd like to confirm.

For example:

Public Function IsStringHello(ByVal test As String) As Boolean 
  Return (String.Compare(test, "Hello") = 0)    
End Function

Calling program:

Dim myWord as String = "Blah"
Dim matchesHello as Boolean = IsStringHello(myWord)

I know passing myWord by value makes a copy of the reference to "Blah", but since I have not tried to change the string itself, would it make another copy of the string on the heap?

+3  A: 

No. it still uses the copy of the reference to the "Blah".
What makes you think, it will?

On a side note, string are interned.

string s = "hello";
string t = "hello";

s & t both refer to the same string (because it is interned). If you modify s or t, it will create a new string, then.

shahkalpesh
I had a strange (foolish?) thought maybe merely making a copy of a reference to an immutable object caused a copy of the object itself, too. I strongly suspected that this was NOT the case, but I wanted to confirm. Great side-note about the "interning" of strings -- I did not know that. Thanks!
vg1890
String *constants* are interned. Strings you create in other ways aren't interned.
Jon Skeet
@JonSkeet - ahhh...so in shahkalpesh's answer above where we assign two references to the same string *literal* does not mean they refer to the same string on the heap?
vg1890
shahkalpesh
A: 

Is short, no. It passes a ref to the string. Only one instance of the string itself.

Scott Weinstein
+5  A: 

By the way, string interning is completely unrelated to that. The rule for passing parameters to functions is the same for all reference types (and really, all types), no matter how they are managed internally.

The rule is simple and you have stated it correctly: pass by value copies the reference, not the target. No heap space is copied here.

Konrad Rudolph
shahkalpesh
Ah right. I cleared the statement up.
Konrad Rudolph
+1  A: 

string is a reference type. If you pass it by value, what you are passing is the value of the reference.

The only way you'd get another copy on the heap would be to change the variable's value.

jlembke
+1  A: 

Passing objects ByVal creates a copy of the pointer, not the object itself. Here's a demonstration:

Module Module1
    Dim original As String = "Hello world"

    Sub PassByReferenceTest(ByVal other As String)
        Console.WriteLine("object.ReferenceEquals(original, other): {0}", _
            Object.ReferenceEquals(original, other))
    End Sub

    Sub Main()
        PassByReferenceTest(original)
        Console.ReadKey(True)
    End Sub
End Module

This program outputs the following:

object.ReferenceEquals(original, other): True

So, the original string and the string we passed by value exist at the same address in memory address. You're not making a copy of the string itself.

Juliet