BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Value Types and Reference Types

C# permits the use of value types, such as enumerations and structures, and reference types, such as objects instantiated from classes. Value types hold their data directly and can be easily copied. Reference types add a layer of indirection.

Value Types

Value types include the integral numeric data types such as "int", other structures and enumerations. When a value type is assigned to a variable, the variable contains that information directly. If a second variable is assigned the value of the first, a copy of the contents is made. The two copies can then be used independently.

This is analogous to the emailing of files as attachments. A user may have a word-processed document that another user wishes to edit. This document can be emailed to the second user as an attachment. Either user can now modify the document in any way without affecting the other's copy.

To illustrate what happens when value type a variable is copied, consider the following diagram.

C# Value Types

In the first step, the variable "a" is declared and assigned a value of 72. You can see this value becoming the contents of the variable in the first column.

In the second step, the variable "b" is declared and assigned the value from variable "a". This value is copied into the contents of the new variable, as can be seen in the second column.

In the third step, the value in variable "a" is modified. The value in variable "b" is unchanged because this variable contains an independent copy of the value. The two variables are not linked in any way.

Reference Types

Reference types include classes of objects. When a reference type is assigned to a variable, the variable does not contain the contents of the object directly. Instead, the object's data is constructed in memory and the variable contains a reference to that memory location. If a second variable is assigned the value of the first, a copy of the reference is made. The two variables now contain independent copies of the reference but in each case the reference points to the same data. If the properties of such an object are changed, the change is apparent through both references.

This is analogous to the emailing of links to files. A user may have a word-processed document, held in a networked folder, that another user wishes to read. The user can email a link to the file to another user. Both users now have a reference to the document in the form of a link to the file in the shared folder. Either user may click the link and edit the text but the link itself remains unchanged. If the other user clicks her copy of the link, she will see the updated document.

To illustrate what happens when reference type a variable is copied, consider the following diagram.

C# Reference Types

In the first step, the object "a" is declared as an instance of the Person class. This is a simple class with a single property representing the person's height in inches. A new area in memory, represented by the "X" column, is allocated to hold the object's contents. Currently the contents are null because the property has not been set. The variable "a" holds a reference to the memory location "X".

In the second step, the Height property of variable "a" is set to 72. The contents of the first column are unmodified because the reference itself has not changed. However, the memory location holding the height property is changed as can be seen in the third column.

In the third step, a second Person variable "p" is declared and assigned the value of variable "a". As these are reference types, the reference to the "X" memory location is copied into variable "b". Both variables have a reference to the same underlying location and both now return a Height property of 72.

In the final step, the Height property of variable "a" is changed to 75. This does not modify the reference within the variable, only the contents of the memory holding the property, seen in the "X" column. As both variables still reference that memory location, both variables will return a Height property of 75.

19 April 2008