(C# Language) Reference and ValueTypes

Value Types and Reference types are two types in C#. If you are familiar with C and C++, then you can think of reference types as that hold address of objects created on a heap memory. In contrast the value types hold an object. These concepts are far too complex for a beginner's tutorial. So I will try to explain them from a practical standpoint.
(Rev. 30-Sep-2022)

Categories | About |     |  

Parveen,

class is a Reference Type

class, interface, delegate and record are used to declare reference types. C# provides three built-in reference types - string, object and dynamic.

Let's write a program that helps us understand the basic concept with an example.

Start by writing the main function. Declare a variable of a class type. Assume that we have a class CMyClass declared somewhere in our project. If you are following this course, then you can obtain the project from the downloads attached to this video.

A mere declaration stores a null into the variable.

Next we have used the new keyword to create an object. The object is created on the garbage collected heap. It's reference is stored in the identifier.


// reference type  
class CMyClass
{
  public int m;
}

class Program
{
  static void Main()
  {
    CMyClass cmc;

    cmc = new CMyClass();

    // stores the same reference 
    CMyClass cmc2 = cmc;

    if(Object.ReferenceEquals(cmc2, cmc))
    {
      // this prints 
      Console.WriteLine("cmc and cmc2: same object");
    }
    else
    {
      Console.WriteLine("cmc and cmc2: different object");
    }
  }
}

After this we have another identifier that stores the previous identifier. This it points to the same object on the heap. Nothing new has been created. Only one object exists.

Lastly, we check whether both the references are equal. The function ReferenceEquals determines if two references point to the same object.

If we run the project we verify that both the variables point to the same object on the heap.

Thus reference types hold reference to the objects on heap.

Video Explanation (see it happen!)

Please watch the following youtube video:

Value Types hold the actual object

Next let us examine the behavior of a value type.

Start writing a program. We have created a namespace, though it's not necessary. Write a simple struct to declare a value type.


// value types copy 

namespace ConsoleApp1
{
  // value type  
  struct SMyStruct
  {
    public int m;
  }

  class Program
  {
    static void Main()
    {
      SMyStruct cmc;

      cmc = new SMyStruct();

      // assignment creates a copy 
      // so two objects exist here 
      SMyStruct cmc2 = cmc;

      if(Object.ReferenceEquals(cmc2, cmc))
      {
        Console.WriteLine("cmc and cmc2: same object");
      }
      else
      {
        // prints this 
        Console.WriteLine("cmc and cmc2: different object");
      }
    }
  }
}

Write the main program class. Add the main function.

Next declare a struct. Create an instance of a struct. Notice we have used the new keyword here also. Next create a variable of the same struct and assign the previous object to this variable. This assignment causes a new object to be created. This is how value types differ from reference types.

Next we can verify the equality of references. We verify that the two objects are different.

Another Example

We shall write another program to understand value types.

First we create an instance of a struct. Next we have set its data member to a value of 10. After that another struct is created from this struct by using an assignment. But since a struct is value type, we get two objects at this point.

Change the value of the first struct to 30 now. Print the value - it should print 30. Lastly, print the value of the second struct - it should print 10. Hence both the structs are different and contain their respective data.

Run the program to verify that the two objects indeed contain their respective data.


static void Main()
{
  SMyStruct cmc = new SMyStruct();

  cmc.m = 10;

  // assignment creates a copy 
  // so two objects exist here 
  SMyStruct cmc2 = cmc;

  // change the first object 
  cmc.m = 30;

  // prints 30 
  Console.WriteLine($"cmc.m = {cmc.m}");

  // prints 10 
  Console.WriteLine($"cmc2.m = {cmc2.m}");
}

More Value Types

All structs are value types. Enumerations are also value types.

Various built-in types are value types.

Integral types such as int, short, long are value types.

floating point numbers such as double, float types are also value types.

bool is a value type, and char is also a value type.


This Blog Post/Article "(C# Language) Reference and ValueTypes" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.