Reference Types vs Value Types

Go to the C# interactive window to take a deeper understanding about this types.

  • Right-click at the project and choose "Initialize Interactive with Project"

Type:

> var result1 = new GameResult();
> var result2 = result1;

Now, we have 2 difference variables, both pointing to the same object in memory.

> result1.GameDate = DateTime.Now;
> result2.GameDate

So, when we change result1, result2 got changed too. But, result1 is Value Type while result2 is Reference Type because it references to result1.

Another example.

> var date1 = new DateTime(1999, 12, 31);
> var date2 = date1;

> date1
  [12/31/1999 12:00:00 AM]
> date2
  [12/31/1999 12:00:00 AM]

> date1 = date1.AddDays(10);
> date1
  [1/10/2000 12:00:00 AM]
> date2
  [12/31/1999 12:00:00 AM]

results matching ""

    No results matching ""