Value type vs reference type

In this chapter you will learn:

  1. What are the difference between value type and reference type
  2. An example of value type
  3. An example for reference type
  4. Set reference value to null

Types and variables

There are two kinds of types in C#:

  • value types
  • reference types.

Variables of value types directly contain their data. Variables of value types have their own copy of the data. Variables of reference types store references to their data(objects). Variables of reference types may reference the same object.

C#'s value types includes:

  • simple types
  • enum types
  • struct types
  • nullable types

C#'s reference types are:

  • class types
  • interface types
  • array types
  • delegate types.

The following table lists the concrete types and their group names:

Signed integralsbyte, short, int, long
Unsigned integralbyte, ushort, uint, ulong
Unicode characterschar
IEEE floating pointfloat, double
High-precision decimaldecimal
Booleanbool
Enumenum EnumTypeName {...}
Structstruct StructTypeName {...}
Nullable value typenullable value

Value type

The variables of value type store the real value, while the reference type variables store the references to the real objects. The following code defines an int type variable. int type is a value type.

using System;/*from  j  a  v a  2s .co  m*/

class Program
{
    static void Main(string[] args)
    {
        int i = 5;
        int j = i;

        i = 3;
        Console.WriteLine("i=" + i);
        Console.WriteLine("j=" + j);
    }
}

The output:

From the outputs we can see that the value of j doesn't change even the value of i is changed. The value of i is copied to j when assigning the i to j.

Reference type

A reference type variable has two parts: the object and its address. A reference type variable stores the address of the object, not the object itself.

The following code creates a Rectangle class, which is a reference type.

using System;/*from  j ava  2  s  .com*/

class Rectangle
{
    public int Width = 5;
    public int Height = 5;
}
class Program
{
    static void Main(string[] args)
    {
        Rectangle r1 = new Rectangle();
        Rectangle r2 = r1;
        Console.WriteLine("r1.Width:" + r1.Width);
        Console.WriteLine("r2.Width:" + r2.Width);

        r1.Width = 10;
        Console.WriteLine("r1.Width:" + r1.Width);
        Console.WriteLine("r2.Width:" + r2.Width);

    }
}

The output:

In the main method two variable r1 and r2 are created. They both point to the same object.

We can see that as we change the width through r1 the r2.Width gets the change as well.

null reference value

A reference type variable can be assigned to null.

null means the reference type variable points to nothing.

using System;/*from  j  av a2  s . co m*/

class Rectangle
{
    public int Width = 5;
    public int Height = 5;
}
class Program
{
    static void Main(string[] args)
    {
        Rectangle r1 = null;

        Console.WriteLine(r1);
    }
}

A value type variable cannot be assigned to null.

using System;//from   j  av  a  2s  . c  o m

class Program
{
    static void Main(string[] args)
    {
        int i = null;

        Console.WriteLine(i);
    }
}

Compiling the code above generates the following error message:

Next chapter...

What you will learn in the next chapter:

  1. What are the literial for value data types
Home » C# Tutorial » Data Types
C# Data Types
Value type vs reference type
Literals
Value default value
Anonymous type