Taken from the text .NET Framework Application Development Foundation (MCTS Exam 70-536):
“Value types are variables that contain their data directly instead of containing a reference to the data stored elsewhere in memory. Instances of value types are stored in an area of memory called the stack, where the runtime can create, read, update and remove them quickly with minimal overhead.”
whereas
“Reference types store the address of their data (a.k.a – a pointer), on the stack. The actual data that the address refers to is stored in an area of memory called the heap. Because reference types represent the address of data rather than the data itself assigning on reference variable to another doesn’t copy the data, but instead creates a second copy of the reference which points to the same memory location on the heap.”
I tend to remember the difference between reference and value types in terms of the kind of objects they are. Value types tend to be things like primitives, such as int, float and double; where reference types are like classes (such as StringBuilder, or ArrayList, or StreamReader).
Additionally, When creating a copy of a value type, if you change the value of one of it’s member variables, it won’t change the value in the original. I demonstrate that with this code:
namespace ValVsRefTypeDemo
{
struct ShipCallNumber
{
public int callNumber;
public ShipCallNumber(int callNum)
{
callNumber = callNum;
}
public override string ToString()
{
return callNumber.ToString();
}
}
class Program
{
static void Main(string[] args)
{
ShipCallNumber enterprise = new ShipCallNumber(1701);
ShipCallNumber voyager = enterprise;
enterprise.callNumber += 1;
voyager.callNumber += 24;
Console.WriteLine("Using Value Type\n-----------------------\nEnterprise: {0}\nVoyager: {1}\n", enterprise, voyager);
}
}
}
which resulted in:
| Using Value Type
———————– Enterprise: 1702 Voyager: 1725
Press any key to continue . . . |
I also know that when we make a copy of the reference type we make a copy of the pointer to the value. So if we change the value it changes the value of any instances of the reference type; as in:
namespace ValVsRefTypeDemo
{
struct ShipCallNumber
{
public int callNumber;
public ShipCallNumber(int callNum)
{
callNumber = callNum;
}
public override string ToString()
{
return callNumber.ToString();
}
}
class CallNumber
{
public int cNumber;
public CallNumber(int _cNumber)
{
cNumber = _cNumber;
}
public override string ToString()
{
return cNumber.ToString();
}
}
class Program
{
static void Main(string[] args)
{
ShipCallNumber enterprise = new ShipCallNumber(1701);
ShipCallNumber voyager = enterprise;
enterprise.callNumber += 1;
voyager.callNumber += 24;
Console.WriteLine("Using Value Type\n-----------------------\nEnterprise: {0}\nVoyager: {1}\n", enterprise, voyager);
//using reference types
CallNumber cNumEnterprise = new CallNumber(1701);
CallNumber cNumVoyager = cNumEnterprise;
cNumEnterprise.cNumber += 1;
cNumVoyager.cNumber += 24;
Console.WriteLine("\n\nUsing Reference Type\n----------------------\nEnterprise call number: {0}\nVoyager call number: {1}\n", cNumEnterprise, cNumVoyager);
}
}
}
Which results in:
| Using Value Type
———————– Enterprise: 1702 Voyager: 1725
Using Reference Type ———————- Enterprise call number: 1726 Voyager call number: 1726
Press any key to continue . . . |
