Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.String - Reference Type

System.String - Reference Type

Scheduled Pinned Locked Moved C#
csharphelpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zecodela
    wrote on last edited by
    #1

    in .net documentation, System.String stated as reference type. so, i supposed it will pass the reference to the method and the modification will affect to the original variable. but, i found it is not true! here is the sample i draft. anyone could give me a help so that i work as call by reference.? private void button1_Click(object sender, System.EventArgs e) { System.String A; System.String B; A = "A for apple"; B = "B for boy"; Sub(A, B); MessageBox.Show("A: "+A+", B: "+B); } private void Sub(System.String a, System.String b) { System.String tmp; tmp = a; a = b; b = tmp; }

    S S L 3 Replies Last reply
    0
    • Z zecodela

      in .net documentation, System.String stated as reference type. so, i supposed it will pass the reference to the method and the modification will affect to the original variable. but, i found it is not true! here is the sample i draft. anyone could give me a help so that i work as call by reference.? private void button1_Click(object sender, System.EventArgs e) { System.String A; System.String B; A = "A for apple"; B = "B for boy"; Sub(A, B); MessageBox.Show("A: "+A+", B: "+B); } private void Sub(System.String a, System.String b) { System.String tmp; tmp = a; a = b; b = tmp; }

      S Offline
      S Offline
      Sujith S
      wrote on last edited by
      #2

      Hi, You need to use 'ref' keyword in the function declaration and while passing the value. ie private void Sub(ref System.String a, ref System.String b){} and Sub(ref A, ref B); Thanks, Sujith

      1 Reply Last reply
      0
      • Z zecodela

        in .net documentation, System.String stated as reference type. so, i supposed it will pass the reference to the method and the modification will affect to the original variable. but, i found it is not true! here is the sample i draft. anyone could give me a help so that i work as call by reference.? private void button1_Click(object sender, System.EventArgs e) { System.String A; System.String B; A = "A for apple"; B = "B for boy"; Sub(A, B); MessageBox.Show("A: "+A+", B: "+B); } private void Sub(System.String a, System.String b) { System.String tmp; tmp = a; a = b; b = tmp; }

        S Offline
        S Offline
        Stefan Troschuetz
        wrote on last edited by
        #3

        If you pass an object of a reference type to a method, all modifications to the method parameter object will affect the original object. For example changing the Text property of a TextBox. That doesn't include changing the reference itself (this is what you do). If you want this to affect your original object you have to use either the out or ref method parameter keyword.


        www.troschuetz.de

        1 Reply Last reply
        0
        • Z zecodela

          in .net documentation, System.String stated as reference type. so, i supposed it will pass the reference to the method and the modification will affect to the original variable. but, i found it is not true! here is the sample i draft. anyone could give me a help so that i work as call by reference.? private void button1_Click(object sender, System.EventArgs e) { System.String A; System.String B; A = "A for apple"; B = "B for boy"; Sub(A, B); MessageBox.Show("A: "+A+", B: "+B); } private void Sub(System.String a, System.String b) { System.String tmp; tmp = a; a = b; b = tmp; }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          From what I am aware of a System.String is a reference type, yes, but it's also immutable - i.e., the character sequence in it cannot be changed once it is created. Methods that appear to change a string only return a new string with the modifications made. Note: everything below here is my interpretation of what is going on. :) When you do something like this:

          string a = "one";
          string b;

          b = a;

          a and b will point to the same location for the string in memory - just like you expect with reference types. The catch is that because a string is immutable, as soon as you do this:

          b = "two";

          The CLR will now remove b's reference to a, create a new String object (with the character sequence "two") and assign that to b. a and b now point to two entirely different System.String's in memory. This process of sharing a reference until someone makes a change helps keep memory usage down whilst keeping a System.String inherantly immutable - two strings will point to each other until one of them changes. When one of them changes, two entirely seperate strings are created. Even when you pass a string by reference (using ref) you still aren't actually modifying the original string. All you are doing is creating a new System.String in memory somewhere and changing the ref'd variable's reference to point to this new string. The old string still exists in memory somewhere, but providing certain conditions are met it's up for garbage collection now. public void Foo() { string one="two"; Bar(ref one); // pass in the refence to the variable "one". Does not pass the string. } public void Bar(ref string inStr) { // create a new System.String in memory, set it to the character // sequence "three", and change inStr's reference to point to this new string. inStr = "three"; } This space for rent! My Blog

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups