Compare the Difference Between Similar Terms

Difference Between Value Type and Reference Type

Key Difference – Value Type vs Reference Type
 

A programming language is designed to given instructions to the computer to perform tasks. The program can manipulate data containing numbers, characters and strings and produce information to the user. A sequence of instructions written in a specific programming language is a program. In programming languages such as C#, there are tokens. Those are keywords, identifiers, literals and operators. The reserved memory locations to store data is known as variables. A variable can store a value of a certain type. The data types can be divided into value type and reference type. The differentiation between value type and reference type depends on two features. Those are where the variables are stored in memory and how they behave with an assignment statement. This article discusses the difference between value type and reference type. The key difference between the value type and reference type is that a value type holds a data value within its own memory space while a reference type holds a pointer to another memory location that holds the data.

CONTENTS

1. Overview and Key Difference
2. What is Value Type
3. What is Reference Type
4. Similarities Between Value Type and Reference Type
5. Side by Side Comparison – Value Type vs Reference Type in Tabular Form
6. Summary

What is Value Type?

The value types can be divided into two categories. They are predefined types and user-defined types. The predefined types are provided by the programming language. They can be numerical, character type or decimal type. The numerical types can be integral types, floating point types and decimal types. Integral types can be further classified into signed and unsigned. The signed integers can store positive or negative number. The sbyte, short, int and long are signed data types.

The sybte is one byte, short is two bytes, int is four bytes, and long is eight bytes. The unsigned type store positive values. The byte, ushort, uint and ulong are the examples for unsigned types. The floating-point types can store a numerical value with a decimal point. The float is to store 32-bit single precision floating point value, and the double is used to store 64-bit single precision floating point. The decimal type is for high precision values. The char data type is used to store a single character in the memory. Sometimes it is required to store true or false value. The Boolean is used for that. Those are some examples of value types provided by the programming language.

The user can also create value types. Two such user-defined value types are structure and enumerations. A structure contains data of different types. It is similar to a class.  The‘struct’ keyword is used to declare a structure variable.  An example of the structure is as follows.

struct Employee {

public int empNo;

public string name;

}

Enumerations are used to increase the readability of the code. It is used to give names to the numbers. The ‘enum’ keyword is used to declare an enumeration. An example of enum is as follows. e.g. – enum Color {black, white, purple}. The enum keyword automatically enumerates a list of words by assigning values starting from 0. According to the above example, black is assigned with 0, white is assigned with 1 and purple is assigned with 2. If it is written as enum Color {black, white=3, purple}, then the black is assigned with 0 and purple is assigned with 4. Those are some examples of value types. They are stored in the stack. When assigning a value to another variable, the value is copied to the new location. Therefore, two copies of the same value exist in the memory.

What is Reference Type?

The reference types can be divided into two categories as user-defined and predefined types. Some examples of user-defined types are classes, interfaces and arrays. A class is used to create objects. A class consists of data members and methods. The data members describe the attributes and methods describe the behaviour. An interface contains only the declaration of the members. The members should be defined by the derived class. An array can store multiple elements of the same data type.

The predefined types are an object type and string type. The object type is the base type for all other user-defined types. The object references are used to bind to an object of any particular type. The string literals are stored in string objects as values. Operations such as copying, comparing and concatenation can be done using them. Those are some examples of a reference type. The reference types are stored on heap.  When assigning one reference variable to another reference variable, only the reference is copied. The actual value remains the same.

What is the Similarity Between Value Type and Reference Type?

What is the Difference Between Value Type and Reference Type?

Value Type vs Reference Type

A value type holds a data value within its own memory space. A reference type holds a pointer to another memory location that holds the data.
 Assignment
In value type, the value is copied to the new location, so there are two identical copies of the same value in the memory. In reference type, the reference is copied while the actual value remains the same.
Stored Location
A value type is stored in the stack. A reference type is stored on the heap.
 Examples
The int, float, double, struct, enum are some examples of value types. The class, array, interface are some examples of reference types.

Summary – Value Type vs Reference Type

The data is stored in reserved memory locations known as variables. Each variable stores a specific data type. The data types can be divided into two categories. They are value type and reference type. The difference between the value type and reference type is that a value type holds a data value within its own memory space while a reference type holds a pointer to another memory location that holds the data.

Reference:

1.Value Type and Reference Type, TutorialsTeacher.com. Available here
2.Balagurusamy, E. Programming in C#, A premier. Fourth ed., McGraw Hill Education (India)Private Limited, 2016.