Skip to main content

Understand the value type and reference type

In previous article we have seen difference between primary key and unique key. Today we will see the difference between value type and reference type.

Let first see what are the value types and what are the reference types.

What are Value Types?
Variables which stores direct data are called value types they are stored on a stack. They contains the actual value rather then reference.
For an example enum, structure int are value types.

What are Reference Types?
Variables which stores reference of actual data are called Reference types. They are stored on heap but contain the address on heap.
For an example Array, class, object, string, delegate, interface are reference type.

Now lets see the difference between both of them.

Before that let me show you the difference between Stack and Heap

Difference between Stack and Heap

Stack Heap
Stack is used for value types. Heap is used for reference types
Values are stored one on another like a stack Values are stored in random dumped into a big space

Difference between value type and reference types

Value Type Reference Type
Value Type are stored on stack Reference Type are stored on heap
It contains actual value It contains reference of a value
For value type memory is allocated at compile time For reference type memory is allocated at run time
Once they go out of scope it will automatically removed from stack, so no need for garbage collector. In reference type it required garbage collector to free the memory.
If value type is null able then only it contains null value, otherwise not Reference Type can contain null values.

That’s it.

Tarun Dudhatra

Tarun is a writer of website. He is a software professional with hands on 10+ years of experience. Generally he writes an article about microsoft technology, Javascript, SQL Server and JQuery. Here he shares his notes, experiences, examples.