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!
REFERENCE TYPE ASSIGNMENT
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();
}
}
VALUE TYPE ASSIGNMENT
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();
}
}