VB.NET: If I pass a String ByVal into a function but do not change the string, do I have one or two strings in memory?

StackOverflow https://stackoverflow.com/questions/1050590

  •  20-08-2019
  •  | 
  •  

Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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

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.

A variable of type System.String effectively holds an "object-ID". Suppose that Object #1934 is a string with the characters "Blah", and you say Dim myWord As String = "Blah". The compiler will then store Object #1934 into myWord. Calling IsStringHello(myWord) would then cause that function to be called with its test parameter equal to Object #1934. In your example, there would be two variables of type System.String in memory--myWord and test, and both would hold the content Object #1934.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top