Sunday, September 28, 2008

What is differnce between reference type and value type?

When we build applications that use variables they are allotted to memory blocks either in stack or heap memory.They can be either refernce type or value type.
Please find my source code below and screen shot next to it explaining what is reference and value type.

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Learnings
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
class classtype
{
public int var;
}
struct stacktype
{
public int var;
}
private void button1_Click(object sender, EventArgs e)
{
stacktype objstk1 = new stacktype();
stacktype objstk2 = objstk1;
objstk2.var = 100;

classtype objcls1 = new classtype();
classtype objcls2 = objcls1;
objcls1.var = 200;

MessageBox.Show("Struct " + objstk1.var + ", " + objstk2.var);
MessageBox.Show("Class " + objcls1.var + ", " + objcls2.var);

}
}
}


Output:














You can observe I have defined a class and a stack.
We know that “new” helps in creating/assigning a memory block.

Variables objstk1 and objstk2 store values assigned to variable (var) in there specific memory locations.

References objcls1 and objcls2 points to a location or stores address of a memory block (heap memory) which is created and has variable var =200.

I am saying objstk1,objstk2 as variables (value variable) and objcls1,objcls2 as refernces (refernce variable) which sounds or says what I mean.


So value type says that it holds real value and reference says that it holds references of memory blocks.


When i say “stacktype objstk2 = objstk1;” then a new memory location is created for objstk2 in stack memory and a separate variable var in it.
When I assign value for var which is holded by objstk1 , obviously will not be assigned for var holded by objstk2.

If I do in this manner then we have same values in var holded by 2 variables.

stacktype objstk1 = new stacktype();
objstk1.var = 100;
stacktype objstk2 = objstk1;


That is first I assigned value to var holded by objstk1 and then assigned objstk1 to objstk2 , which does copy content from objstk1 to objstk2 (shallow copy).



Coming to refernce types,
When I say “classtype objcls1 = new classtype();” then
At run time a memory block is created in heap memory and its address is assigned to a stack memory block alias objcls1 which is refernce type variable.


When I say “classtype objcls2 = objcls1;” then the same content from objcls1 is copied to new memory block instack memory alias objcls2.


So both refernce variables hold same memory blocks address which is created in heap memory.

So when I assign value to var variable through objcls1 , and can retrieve same value from var holded by objcls2 , since both objcls1 and objcls2 hold same memory block address (heap memory).

Below screen shot helps to understand visually.






No comments: