Difference Between Value vs Reference Type Variables

Difference between Value vs Reference Type variables

Introduction

Hello! Hope you must have gone through our most interesting
.net core interview questions,
mvc,
ASP.Net
and
c# articles. Now, we will see the difference between value type and reference type
variables. All the data Types in .NET
are either handled by Value Type or Reference Type variables.

A Value Type variable possesses the data within its memory area and a
Reference Type variable carries a pointer to another memory area that carries
the real data. The Value Type variables get stored in the stack while
Reference Type variables get stored in the heap.

Value Type variable:

A Value Type variable stores its contents in the memory which is allotted on
the stack. Often, when we create a Value Type variable, a unique space in
memory is allotted to get the value stored and that variable carries a value
certainly. If you assign this variable to another variable, the value will be
copied directly and both variables start functioning independently. Previously
declared data types, structures, and enums are also considered value types and
perform in the same approach. Also, Value type variables can be produced at
the time of compilation and can be stored in stack memory. The garbage
collector is unable to access this stack because of this variable.

int x=21;

e.g.

Here the value 21 is stored in the stack memory area.

Reference Type variable

Reference Type variables are utilized by a reference that possesses a
reference or address to another object but not the same object itself. Because
reference-type variables depict the location details of that variable rather
than the same data itself, assigning a specific reference variable to another
one, doesn’t duplicate the data. Instead, it generates a 2nd copy of the
reference variable, which relates to the same particular location of the
specific heap as the actual value. Reference Type variables get stored in
various areas of memory named heap. It means when a reference type variable is
no longer utilized, it can be captioned for garbage collection.


Examples of reference-type variables are
Classes, Arrays,
Objects, Interfaces, Indexers, etc.

e.g.

int iArray[] =new int[31];

In the above example with code, the space desired for the 31 integers that
compose up the array is allotted on the heap.

Heap and Stack

Heap is utilized for dynamic memory allotment, and Stack is utilized for
static memory allotment, but both are stored in the RAM of pc.

Let’s discuss some more value type and reference type variables.  

Class and Struct

Class
is considered as a variable of pass-by-reference and Struct is considered as a
variable of pass-by-copy, it indicates that Class is a reference category
variable and its object is built on the heap memory whereas the structure is a
value category variable and its object is built on the stack memory.

Dynamic Data Type

The
dynamic data type
concept brings some new features to C# 4. However, Dynamic Type depicts that
you will be able to store any kind of value or data in the dynamic variable
because verifying of data type for dynamic categories of variables occurs at
run-time.

Conclusion

I hope the above article on the differentiation of value type and reference
type could help freshers or beginners in the development field. These
variables are a significant part of database interaction for end-users.

By Janet J

Leave a Reply