title: Question about c# and string outside the class or function vs string inside a class or function
body: <p>I know that string is a reference type, but I'm having problem understanding why string outside the &quot;refTest&quot; class or function won't behave like the one inside the class &quot;refTest&quot;. I mean if I have 2 string named &quot;name&quot; and &quot;name2&quot;, and both have the same value since name2 has a copy of name, it will output the same value &quot;A&quot;, but then I change the second string value to &quot;B&quot; and this time name and name2 have different value &quot;A&quot; and &quot;B&quot;, and then when I do the same thing with a string that is from a class &quot;refTest&quot; it will output name1 and name2 the same value. Aren't both string reference type? Which means they have a pointer on a heap. Right?
  I'm getting confused on why string outside the class &quot;refTest&quot; will output a different outcome compared to the one inside the &quot;refTest&quot; class.</p>
  <p>Or it is that everytime I declare a string outside the &quot;refTest&quot; class, it will be stored on a different address (heap address)?</p>
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          
          string name = &quot;A&quot;;
          string name2 = name;
          Console.WriteLine(name2 + &quot; &quot; + name);
          // &gt;&gt; A A
          name2 = &quot;B&quot;;
          Console.WriteLine(name2 + &quot; &quot; + name);
          // &gt;&gt; A B
  
  
          refTest test = new refTest();
          test.rTName = &quot;A&quot;;
          Console.WriteLine(test.rName);
          // &gt;&gt; A
          refTest test2 = test;
          test2.rName = &quot;B&quot;;
          Console.WriteLine($&quot;test.rName = {test.rName}, test2.rName = {test2.rName}&quot;);
          // &gt;&gt; test.rName = B, test2.rName = B
          Console.ReadLine();
      }
  
  }
  
  
  class refTest
  {
      public string rName;
  }
  </code></pre>