| ||||||
| ||||||

Reference TypesGoals:
OverviewArrays and classes are reference types. This means that two things are involved when dealing with instances: a reference and the object itself. A reference is used as a handle to manipulate the object. This organization has a number of implications for how reference types behave during operations such as assignment and parameter passing. Reference declarationA variable declaration of a class type yields only a reference, it does not create an object of that class. References are often called "handles" or "pointers" since they are used to manipulate or point at objects.
Object creation
Objects are created using the
It is also possible to do both the reference declaration and object creation in a single line of code as shown below.
Reference assignmentAssigning references copies only the reference, it does not copy the data inside the object. To see this, consider the following code which shows two references each referring to a separate object.
Now consider performing an assignment such as "
null
The link between a reference and an object can be broken by setting the reference
to
Attempting to use a null reference is an error. The error is detected at runtime
by the CLR. The CLR stops the operation and throws a
In order to avoid a
It is common to use a reference type as a field. For example, a parent might hold a
reference to each child, a highway would need to keep references to each city it
connects, and a circle must have a reference to its center point. Recall that each
field gets assigned a default value based on its type: numeric fields get set to
zero, Boolean fields are set to false, etc. In a similar fashion, reference fields
are set to
Reference parametersWhen reference types are passed as parameters, only the reference is passed and the object is not copied. The figure below illustrates the situation.
Passing a reference has two important implications. First, reference parameters can be quite efficient since only a reference is passed and there is no need to copy all the data inside the object. Second, because the method holds a reference to the original object, any changes it makes modify that object. The effects of these changes will be visible to the client code after the method returns. Array
Arrays are reference types just like class types so they are implemented as
reference/object pairs. An array declaration creates only a reference. The associated
array object must be created using the
It is common to declare the reference and create the array in a single line of code. This style is a bit cleaner since the reference is created and then immediately bound to an array object. The figure below shows the two steps being performed together.
Because arrays are references type, they behave just like class types in operations such as assignment and parameter passing. For example, assigning one array to another copies the reference and not the data inside the array. This situation is shown below.
As a final variation on arrays, we will look at a slightly more complicated case:
an array of references. Creating an array of a class type with an expression such
as "
Memory management
In the last few modules we have allocated quite a few objects using
More technically, memory for objects of reference types comes from an area of memory
called the "managed heap". The .NET heap is called "managed" since the CLR takes
responsibility for managing the allocation and reclamation of the memory. Operator
The garbage collector has quite a difficult job since it must determine which objects are still active and which objects are no longer in use by the program. To do this, the garbage collector examines all the "root" references such as live local variables and static fields. It follows these references and marks any objects it finds as being still active. After all live objects have been marked, the garbage collector assumes all other objects are no longer in use and recycles the memory they occupy. The basic idea is illustrated in the figure below.
The CLR controls the timing of garbage collection. The common advice is that the
CLR and the garbage collector are intelligent enough to do the job without any
interference from the programmer. However, the
Despite the sophisticated support for memory management offered by the CLR, it is
still possible for a program to allocate so many objects that it runs out of
available memory. When this happens, the CLR will notify the program by throwing
an This material is excerpted from the Programming C# course offered by DevelopMentor. |