When I pass a string to a function, is a pointer to the string’s contents passed, or is the entire string passed to the function on the stack like a struct would be?
When I pass a string to a function, is a pointer to the string’s
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A reference is passed; however, it’s not technically passed by reference. This is a subtle, but very important distinction. Consider the following code:
There are three things you need to know to understand what happens here:
strMainisn’t passed by reference. It’s a reference type, but the reference itself is passed by value. Any time you pass a parameter without therefkeyword (not countingoutparameters), you’ve passed something by value.So that must mean you’re…passing a reference by value. Since it’s a reference type, only the reference was copied onto the stack. But what does that mean?
Passing reference types by value: You’re already doing it
C# variables are either reference types or value types. C# parameters are either passed by reference or passed by value. Terminology is a problem here; these sound like the same thing, but they’re not.
If you pass a parameter of ANY type, and you don’t use the
refkeyword, then you’ve passed it by value. If you’ve passed it by value, what you really passed was a copy. But if the parameter was a reference type, then the thing you copied was the reference, not whatever it was pointing at.Here’s the first line of the
Mainmethod:We’ve created two things on this line: a string with the value
mainstored off in memory somewhere, and a reference variable calledstrMainpointing to it.Now we pass that reference to
DoSomething. We’ve passed it by value, so that means we made a copy. It’s a reference type, so that means we copied the reference, not the string itself. Now we have two references that each point to the same value in memory.Inside the callee
Here’s the top of the
DoSomethingmethod:No
refkeyword, sostrLocalandstrMainare two different references pointing at the same value. If we reassignstrLocal……we haven’t changed the stored value; we took the reference called
strLocaland aimed it at a brand new string. What happens tostrMainwhen we do that? Nothing. It’s still pointing at the old string.Immutability
Let’s change the scenario for a second. Imagine we aren’t working with strings, but some mutable reference type, like a class you’ve created.
If you follow the reference
objLocalto the object it points to, you can change its properties:There’s still only one
MutableThingin memory, and both the copied reference and the original reference still point to it. The properties of theMutableThingitself have changed:Ah, but strings are immutable! There’s no
ChangeMeproperty to set. You can’t dostrLocal[3] = 'H'in C# like you could with a C-stylechararray; you have to construct a whole new string instead. The only way to changestrLocalis to point the reference at another string, and that means nothing you do tostrLocalcan affectstrMain. The value is immutable, and the reference is a copy.Passing a reference by reference
To prove there’s a difference, here’s what happens when you pass a reference by reference:
This time, the string in
Mainreally does get changed because you passed the reference without copying it on the stack.So even though strings are reference types, passing them by value means whatever goes on in the callee won’t affect the string in the caller. But since they are reference types, you don’t have to copy the entire string in memory when you want to pass it around.
Further resources: