Home > .NET Framework > Value Types and Reference Types

Value Types and Reference Types

Value Types and Reference Types

Value Types

Value types have value semantics, meaning what you have is the value.

int i = 3; means the value of 3 is stored in the variable i.  Examples of value types include in built data types like int, double, float, datetime etc (barring string, delegate). Examples user-defined value types include enums and structures. All value types inherit from System.ValueType either directly or indirectly. Value types are sealed by default, you cannot inherit a value type from another value type. Value type methods cannot be virtual or abstract. Value types cannot have finalizers. Value types cannot have a user-defined default constructor.

 

Reference Types

Reference types store a reference to the actual value.  For example

public class Person

{

            private string name;

            private int age;

           

            public string Name

            {

                        get { return name; }

                        set { name = value; }

            }

 

            public int Age

            {

                        get { return age; }

                        set { age = value }

            }

}

 

public class Program

{

            public static void Main()

            {

                        Persona p = new Person();

                        p.Name = “Sendhil”;

                        p.Age = 28;

                        Person anotherPerson;

                        anotherPerson = p;

                        anotherPerson.Age = 29;

            }

}

Person is reference type. p and anotherPerson are references to the person instance. In this case both p and anotherPerson point to or refer to the same person instance on the heap.  Built-in types like string, delegate and user-defined types like classes, interfaces are examples of reference types. Reference types are always allocated on the Heap.

 

Common Mis-conception

There is always a misconception like value types are always allocated on the stack. This is not true. Value types cannot be allocated as distinct entities on the CLR heap never-the-less or however, value types can be allocated on the heap in the following circumstances:

  • An array of value types
  • A member variable on a reference type

A value type is allocated on the stack when it is a local variable (declared within a method).

What about a reference type which is a local variable. The reference (the object handle) is allocated on the stack whereas the actual object instance is allocated on the heap.

 

A weird scenario

What will be the case when a value type implements an interface? We all know that interface is a reference type. Suppose my struct implements an interface. How does this work? When you have a struct which implements an interface and you use the interface reference to call a method a box operation happens and the value type is boxed into a reference type and placed on the heap. (More on boxing and un-boxing later)

 

To summarize the differences:

 

Value Types

Reference Types

Directly store the value

A reference to the value is what you have.

Inherit directly or indirectly from System.ValueType

No such thing

Sealed

Not sealed unless specified

No virtual dispatch, no abstract methods

Virtual dispatch and abstract method support

No default constructors

Default constructor support

Cannot be allocated as distinct entities on the heap

Always allocated on the heap

Can’t have finalizers

Can have finalizers

Can’t be assigned to null

Can be null

 

 

Also value types and reference types have a slight difference when passed as parameters. We will see that in a separate article. Also we’ll see boxing and un-boxing in a separate article.

Update

A good write-up on the same topic:

Via Amit
 

Advertisements
Categories: .NET Framework
  1. Kumaresan
    November 26, 2007 at 6:46 am

    What about a Nullable int..
    I thought int? (Nullable int) is an Value type..

  1. November 19, 2010 at 11:01 pm
  2. November 19, 2010 at 11:10 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: