I have the following code (note the code below doesnt update the property)
private void queryResultsFilePath_Click(object sender, EventArgs e)
{
Library.SProc.Browse browser = new Browse();
browser.selectFile(QueryResultFilePath);
}
and
public class Browse
{
public void selectFile(string propertyName)
{
...
propertyName = browserWindow.FileName;
}
}
Now i realise that i need to change the second method so that it returns a string and manually assign it to the property in the first example.
What im unsure of is that i thought that when i assigned a ref type as an actual parameter of a method, a copy of its value on the stack (ie its memory address in the heap) was copied to the new location on the stack for the methods formal parameter, so they are both pointing to the same memory address on the heap. So when i changed the value of the formal parameter, it would actually change the value stored on the heap and thus the actual parameters value.
Obviously im missing something since im having to return a string and manually assign it to the property. If someone could point out what ive misunderstood id appreciate it.
Thanks.
I believe the missing piece here is: strings are immutable.
Although you pass it by reference, as soon as anything attempts to mutate the string, a new string is created leaving the old one intact.
I believe it is the only reference type that has enforced immutability.
From MSDN:
Further reading:
http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/e755cbcd-4b09-4a61-b31f-e46e48d1b2eb
If you wish the method to “change” the caller’s string then you can simulate this using the
refkeyword:In this example, the parameter
propertyNamewill be assigned to in the method, because ofrefbeing used, this also changes the string that the caller is pointing to. Note here that immutability is still enforced.propertyNameused to point to string A, but after assignment now points to string B – the old string A is now unreferenced and will be garbage collected (but importantly still exists and wasn’t changed – immutable). If therefkeyword wasn’t used, the caller would still point at A and the method would point at B. However, because therefkeyword was used the callers variable now points to string B.This is the same effect as the following example:
If you try the above without the
refkeyword,classRefwould still point to an object containing “A” even though the class was passed by reference.Don’t get confused between string semantics and
refsemantics. And also don’t get confused between passing something by reference and assignment. Stuff is technically never passed by reference, the pointer to the object on the heap is passed by value – hencerefon a reference type has the behaviour specified above. Also hence not usingrefwill not allow a new assignment to be “shared” between caller and method, the method has received its own copy of the pointer to the object on the heap, dereferencing the pointer has the usual effect (looking at the same underlying object), but assigning to the pointer will not affect the callers copy of the pointer.