Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7973619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:09:27+00:00 2026-06-04T08:09:27+00:00

I have the following code (note the code below doesnt update the property) private

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:09:28+00:00Added an answer on June 4, 2026 at 8:09 am

    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:

    Strings are immutable–the contents of a string object cannot be
    changed after the object is created, although the syntax makes it
    appear as if you can do this. For example, when you write this code,
    the compiler actually creates a new string object to hold the new
    sequence of characters, and that new object is assigned to b. The
    string “h” is then eligible for garbage collection.

    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 ref keyword:

    public void SelectFile(ref string propertyName)
    {
        propertyName = browserWindow.FileName;  
    }
    

    In this example, the parameter propertyName will be assigned to in the method, because of ref being used, this also changes the string that the caller is pointing to. Note here that immutability is still enforced. propertyName used 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 the ref keyword wasn’t used, the caller would still point at A and the method would point at B. However, because the ref keyword was used the callers variable now points to string B.

    This is the same effect as the following example:

    static void Main(string[] args)
    {
        MyClass classRef = new MyClass("A");
        PointToANewClass(ref classRef);
        // classRef now points to a brand new instance containing "B".
    }
    
    public static void PointToANewClass(ref MyClass classRef)
    {
        classRef = new MyClass("B");
    }
    

    If you try the above without the ref keyword, classRef would still point to an object containing “A” even though the class was passed by reference.

    Don’t get confused between string semantics and ref semantics. 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 – hence ref on a reference type has the behaviour specified above. Also hence not using ref will 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code: private void ProcessQueue() { foreach (MessageQueueItem item in GetNextQueuedItem()) PerformAction(item);
I have the following code below to generate an OpenSSL RSA public and private
I have the following HTML code (note no closing tags on <\option> ) generated
I have following code: public static void ProcessStep(Action action) { //do something here if
I have the following code: #include <iostream> #include <string> void main() { std::string str;
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a
I have following code class User attr_accessor :name end u = User.new u.name =
i have following code to show div on page <div id=all_users> <div id=user11 userid=11

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.