Value Types and Reference Types - Reference Types
(Page 3 of 4 )
Reference Types: A reference type, on the other hand, does not store the data itself. Instead, reference types store the address of the data. The address is also referred to as a pointer. The actual data the address refers to is stored in an area of memory called the heap. Here is an illustration:

A reference type is a class, an interface, an array type, or a delegate type. Like declaring any variable, to declare a variable of a reference type, you start with the type, followed by the variable name, such as:
Employee e;
This declaration is not sufficient, however. So fareis referencing nothing. In C# terminology, we say thatecurrently contains the valuenull. This means that we cannot access anyEmployeemember (properties, methods, etc.). To create a meaningfulEmployeeobject with which we can interact, we need thenewoperator and anEmployeeconstructor:
Thenewoperator allocates a block of memory for the object. Unlike C++, it is not the responsibility of the programmer to return this block of memory to the heap. The CLR’s garbage collector checks for objects no longer referenced and cleans them up (frees the memory).
TheEmployeeconstructor is a special method that has the same name as the class. Its purpose is to initialize the class members.
Armed with these two new facts, we can now create anEmployeeobject:
Employee e = new Employee();
Here is a graphical interpretation of the preceding statement:

Now, we can access the Employee object. We use the dot (.) operator to do so:
e.Name; //property
e.GiveRaise(0.05); //method
As stated earlier, the variable e contains the address of the Employee object (not the object itself). If we declare anotherEmployeeobject and assign it the value ofe, we will end up with another reference to the sameEmployeeobject thateis currently referencing (we will NOT have anotherEmployeeobject). After all, we are assigning references (or memory addresses).
Employee e2 = e;
The graphical representation will be as such:

So, any changes that we make to the Employee object through one reference (saye2) will be seen by the other reference (e). Note: if you are not interested in this behavior, and you want two separate Employee objects, then you should implement an interface calledClonable. This, however, is outside the scope of this article.
To see the use of classes in action, let’s take the structure example we used at the beginning of this article and replace the structure with a class. Here is how the code will look:
class Program
{
static voidMain(string[] args)
{
Pointp = new Point();
p.x = 2;
p.y = 7;
Console.WriteLine("p-> "+ p);
Pointp2 = p;
Console.WriteLine("p2-> "+ p2);
Console.WriteLine("nAfter changing the Point through p2:n");
//change the point through p2
p2.x = 12;
Console.WriteLine("p2-> "+ p2);
Console.WriteLine("p-> "+ p);
}
}
class Point
{
public intx;
public inty;
public override stringToString()
{
return string.Format("[{0},{1}]", x, y);
}
}
Output:
p-> [2,7]
p2-> [2,7]
After changing the Point through p2:
P2-> [12,7]
p-> [12,7]
Notice that sincepandp2are referencing the same object, when we changed the value ofxusingp2, the change is also reflected thoughp.
Next: Structures >>
More C# Articles
More By Ayad Boudiab