Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Reference type assignment VS value type assignment
    text
    copied!<p>I understand the theoretical concept that assigning one reference type variable to another, only the reference is copied, not the object. assigning one value type variable to another, the object is copied. But I cannot spot the different in the code. would someone kindly point out the difference between the following two code blocks? Thank you!</p> <p>REFERENCE TYPE ASSIGNMENT</p> <pre><code>using System; class Employee { private string m_name; public string Name { get { return m_name; } set { m_name = value; } } } class Program { static void Main() { Employee joe = new Employee(); joe.Name = "Joe"; Employee bob = new Employee(); bob.Name = "Bob"; Console.WriteLine("Original Employee Values:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); // assign joe reference to bob variable bob = joe; Console.WriteLine(); Console.WriteLine("Values After Reference Assignment:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); joe.Name = "Bobbi Jo"; Console.WriteLine(); Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Name); Console.WriteLine("bob = " + bob.Name); Console.ReadKey(); } } </code></pre> <p>VALUE TYPE ASSIGNMENT</p> <pre><code>using System; struct Height { private int m_inches; public int Inches { get { return m_inches; } set { m_inches = value; } } } class Program { static void Main() { Height joe = new Height(); joe.Inches = 71; Height bob = new Height(); bob.Inches = 59; Console.WriteLine("Original Height Values:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); // assign joe reference to bob variable bob = joe; Console.WriteLine(); Console.WriteLine("Values After Value Assignment:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); joe.Inches = 65; Console.WriteLine(); Console.WriteLine("Values After Changing One Instance:"); Console.WriteLine("joe = " + joe.Inches); Console.WriteLine("bob = " + bob.Inches); Console.ReadKey(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload