3

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();
    }
}
ValidfroM
  • 2,626
  • 3
  • 30
  • 41
user133466
  • 3,391
  • 18
  • 63
  • 92

6 Answers6

5

Well, the obvious difference is that with the class example, it appears both joe and bob changed in the last part there, to the same value.

In the struct example, they keep their separate values, simply because each variable is a whole struct value by itself, not just a reference to a common object in memory somewhere.

The main code-wise difference being the type you use, class or struct, this dictates whether you're creating a reference type or a value type.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
3

One is a structure and the other is a class. This seems like an overly complicated example involving more than just value and reference differences but the differences between classes and structs as well.

When one struct is assigned to another a copy is made.
When one class is assigned to another only the reference changes.

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
2

//Reference Type

        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        sb2 = sb1;
        sb1.Append("hello");
        sb1.Append("world");
        Console.WriteLine(sb2);
        // Output Hello World


        // value Type
        int x=100;            
        int y = x;
        x = 30;            
        Console.WriteLine(y);

        // Out put 100 instead of 30
pratik
  • 21
  • 1
1

The first code snippet contains Employee which is a class and in the second one, Employee is a struct

Victor Hurdugaci
  • 28,177
  • 5
  • 87
  • 103
1

To see the difference more clearly, try something like

joe.Inches = 71;
bob.Inches = 59;
...

// assign joe reference value to bob variable

bob = joe;
joe.Inches = 62;

// write bob and joe

And do something similar in the reference-type example.

You will be able to demonstrate that in the second examples there are two different instances of Height, while in the first example there is only one (shared) instance of Employee.

H H
  • 263,252
  • 30
  • 330
  • 514
1

On my system, those two code blocks produce the following output:

Original Employee Values:

joe = Joe
bob = Bob

Values After Reference Assignment:
joe = Joe
bob = Joe

Values After Changing One Instance:
joe = Bobbi Jo
bob = Bobbi Jo

...and...

Original Height            Values:
joe = 71
bob = 59

Values After            Value Assignment:
joe = 71
bob = 71

Values After            Changing One Instance:
joe = 65
bob = 71

The difference seems self-evident. In the first sample, changing one of the values affects both references, because they are both referencing the same underlying object. In the second sample, changing one of the objects affects only that object, because when value types are copied, each object receives it own private copy. When you say bob = joe; in the second sample, you;re not assigning a reference (the comment is misleading). What you're doing is creating a new instance of the Height struct, copying all the values from joe and storing the new object as bob.

Tim Long
  • 13,508
  • 19
  • 79
  • 147