Value Types and Reference Types

This is one topic which you would find in many programming languages. The concept is the same for all only the syntax or the implementation may vary. So, let’s begin with Value Type.

In simple terms, it is a variable which stores a value. The data type of the variables can be the built in data types supported by a programming language or user defined data types. In case of the C# (pronounced as C sharp)  programming language, it would be the struct typed data types. For Example- Int, Boolean, Float, Long, Char, Decimal and Byte.

Whereas reference types are variables which store a reference or an address. Examples of Reference Types would be Classes, Arrays and Strings .

 

Now that you have a basic understanding of what it is, let’s see how they are stored in memory.

Value Types are defined in the Stack  portion of the memory. The life of a Value Type in that memory location is short lived. Once the variable goes out of scope during the execution of the program, it is automatically removed or deleted from the memory.

Reference Types are stored in the Heap portion of the memory. The life of a reference type is longer and it remains in memory till the termination of the program. Unlike Value Types, it is not removed automatically and it needs the assistance of another resource. It is called the Garbage Collector.

Hope you have got some understanding of the two types which we talked about today.

Advertisements

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