Value type and reference type in Dot net

Value types are types which hold both data and the memory on the same location. While a Reference type has a pointer which points to the memory location.

Below is a simple integer data type with name ‘i’ whose value is assigned to an other integer data type with name ‘j’. Both these memory values are allocated on the stack.

When we assign the ‘int’ value to the other ‘int’ value it creates a complete different copy. In other word if you change either of them the other does not change. These kinds of data types are called as ‘Value types’.


Fig 1: Values allocating on the stack.


When we create an object and when we assign one object to the other object, they both point to the same memory location as show in the below code snippet. So when we assign ‘obj’ to ‘obj1’ they both point to the same memory location.

In other words if we change one of them the other object is also affected this is termed as ‘Reference types’.










 Fig 2: Showing When we create an object and when we assign one object to the other object, they both point to the same memory location.

So which data types are ref type and value type?

In .NET depending on data types the variable is either assigned on the stack or on the heap. ‘String’ and ‘Objects’ are reference types and any other .NET primitive data types are assigned on the stack. Below figure explains the same in a more detail manner.



Refrence -
codeproject

****ATCAPH.com****

Comments

Popular posts from this blog

Artificial Intelligence

Smart Card

Biometrics