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 8491041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:18:02+00:00 2026-06-10T22:18:02+00:00

With this code: void SomeMethod(string str) { string tmpStr = … Read some value

  • 0

With this code:

 void SomeMethod(string str)
 {
      string tmpStr = ... Read some value from file ... 

      if( !tmpStr.Empty() ) 
           str = tmpStr;

 }

When I call this method with a simple string just created inside the main subroutine:

 string localString = "";
 SomeMethod( localString );

The value that I get back in localString is an empty event when I see in debug that in the method SomeMethod the variable tmpStr is not empty and the variable str in the method is full with chars.

If I change the SomeMethod signature to be:

SomeMethod(ref string str) 

I see the right result.

But string is a reference type, so why do I need in this case to send it with ref?
and why I get the result that I expected only when I call:

SomeMethod(string str) 
  • 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-10T22:18:04+00:00Added an answer on June 10, 2026 at 10:18 pm

    It’s not just that the string is immutable, but also that while string is a reference type, a reference itself is not. That is, you are passing the string by reference, but the reference itself is passed by value.

    Thus, for reference types, you can modify the object that is referred to by the parameter (as long as it’s modifiable), but you cannot modify what the argument refers to unless you pass it by reference.

    So, when you try to change what the string variable refers to:

    str = tmpStr;
    

    It changes what str refers to locally, but does not affect what the original argument localString refers to.

    Think of it this way, let’s say that the argument localString refers to an object at location 1000:

    localString 
    +---------------+                      1000
    |      1000     |   -----------------> +---------------+
    +---------------+                      | Count: 1      |
                                           | Value: ""     |
                                           +---------------+
    

    Then when we pass localString to the method, it creates a copy of the reference (as str) and updates the reference count…

    localString 
    +---------------+                      1000
    |      1000     |   -----------------> +---------------+
    +---------------+                      | Count: 2      |
                                           | Value: ""     |
    str                                    +---------------+
    +---------------+                        ^
    |      1000     |   ---------------------+
    +---------------+
    

    Then, when you assign str to a new string, it modifies the reference str but not localString:

    localString
    +---------------+                      1000
    |      1000     |   -----------------> +---------------+
    +---------------+                      | Count: 1      |
                                           | Value: ""     |
    str                                    +---------------+
    +---------------+                          2500
    |      2500     |   ---------------------> +---------------+
    +---------------+                          | Count: 1      |
                                               | Value: ...    |
                                               +---------------+
    

    So your modification of str only changed what str refers to, not the original refernce localString, if you want to change that, then you pass by reference, which means that str is a reference back to the original argument (much like a ptr to a ptr):

    localString
    +---------------+                      1000
    |      2500     |  ------------------> +---------------+
    +---------------+                      | Count: 2      |
            ^                              | Value: ""     |
    str     |                              +---------------+
    +---------------+       
    |               |       
    +---------------+                                                              
    

    Now, when you change str it changes the refernce localString as well:

    localString
    +---------------+                      1000
    |      1000     |  -----+              +---------------+
    +---------------+       |              | Count: 0      |
            ^               |              | Value: ""     |
    str     |               |              +---------------+
    +---------------+       |                  2500
    |               |       +----------------> +---------------+
    +---------------+                          | Count: 1      |
                                               | Value: ...    |
                                               +---------------+
    

    And then, of course, the original string (assuming nothing else refers to it as in this example) can be garbage collected…

    So, if you really want to modify the string parameter, pass it by ref or out, or you can return the new mutated version, or store in an instance member (though pass-by-instance-member is a higher order of coupling and can cause other issues…).

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

Sidebar

Related Questions

Im getting data from a server by executing this code: - (void) sendGeneral:(NSString *)
I have this JSON response string: {d:{\ID_usuario\:\000130\,\Nombre\:null,\Vipxlo\:0,\Provmun\:null,\Descuentos\:null,\Listaviplocal\:null}}` With this code: - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
Consider this code: void res(int a,int n) { printf(%d %d, ,a,n); } void main(void)
I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);
I run this code: - (void)unitButtonButtonTapped:(id)sender { [_label setString:@Last button: Unembossed square]; MilitaryUnits *target
I'm executing this code: void update_process(PROCSP * process){ int state; printf(process.pid = %d\n, process->pid);
Now i ve got this code: -(void)getContacts{ ABAddressBookRef currentAddressBook = ABAddressBookCreate(); if (currentAddressBook) {
I'm searching Twitter for tweets with this code: - (void)fetchTweets { NSURL *url =
I have implemented correctly bump's api, and added this code: - (void) configureBump {
With this code: private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { const int TICKETSOURCE_COLUMN =

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.