|
|
12 Module 8: Using Reference-Type Variables
Using References as Method Parameters
n References Can Be Used as Parameters
l When passed by reference, data being referenced may
be changed
static void PassCoordinateByValue(coordinate c)
{
c.x++; c.y++;
}
static void PassCoordinateByValue(coordinate c)
{
c.x++; c.y++;
}
loc.x = 2; loc.y = 3;
PassCoordinateByValue(loc);
Console.WriteLine(loc.x + " , " + loc.y);
loc.x = 2; loc.y = 3;
PassCoordinateByValue(loc);
Console.WriteLine(loc.x + " , " + loc.y);
22 33 33 44
??
??
You can pass reference variables in and out of a method.
References and Methods
You can pass reference variables into methods as parameters by using any of
the three calling mechanisms:
n By value
n By reference
n Output parameters
The following example shows a method that passes three coordinate references.
The first is passed by value, the second is passed by reference, and the third is
an output parameter. The return value of the method is a coordinate reference.
static coordinate Example(
coordinate ca,
ref coordinate cb,
out coordinate cc)
{
// ...
}
Module 8: Using Reference-Type Variables 13
Passing References by Value
When you use a reference variable as a value parameter, the method receives a
copy of the reference. This means that for the duration of the call there are two
references referencing the same object. It also means that any changes to the
method parameter cannot affect the calling reference. For example, the
following code displays the values 0 , 0:
static void PassCoordinateByValue(coordinate c)
{
c = new coordinate( );
c.x = c.y = 22.22;
}
coordinate loc = new coordinate( );
PassCoordinateByValue(loc);
Console.WriteLine(loc.x + " , " + loc.y);
Passing References by Reference
When you use a reference variable as a ref parameter, the method receives the
actual reference variable. In contrast to passing by value, in this case there is
only one reference. The method does not make its own copy. This means that
any changes to the method parameter will affect the calling reference. For
example, the following code displays the values 33.33 , 33.33:
static void PassCoordinateByRef(ref coordinate c)
{
c = new coordinate( );
c.x = c.y = 33.33;
}
coordinate loc = new coordinate( );
PassCoordinateByRef(ref loc);
Console.WriteLine(loc.x + "," + loc.y);
Passing References by Output
When you use a reference variable as an out parameter, the method receives the
actual reference variable. In contrast to passing by value, in this case there is
only one reference. The method does not make its own copy. Passing by out is
similar to passing by ref except that the method must assign to the out
parameter. For example, the following code displays the values 44.44 , 44.44:
static void PassCoordinateByOut(out coordinate c)
{
c = new coordinate( );
c.x = c.y = 44.44;
}
coordinate loc = new coordinate( );
PassCoordinateByOut(out loc);
Console.WriteLine(loc.x + "," + loc.y);
14 Module 8: Using Reference-Type Variables
Passing References into Methods
Variables of reference types do not hold the value directly, but hold a reference
to the value instead. This also applies to method parameters, and this means that
the pass-by-value mechanism can produce unexpected results.
Using the coordinate class as an example, consider the following method:
static void PassCoordinateByValue(coordinate c)
{
c.x++;
c.y++;
}
The coordinate parameter c is passed by value. In the method, both the x and y
member variables are incremented. Now consider the following code that calls
the PassCoordinateByValue method:
coordinate loc = new coordinate( );
loc.x = 2;
loc.y = 3;
PassCoordinateByValue(loc);
Console.WriteLine(loc.x + " , " + loc.y);
The output of this code is the following:
3 , 4
This shows that the values referenced by loc have been changed by the method.
This might seem to be in conflict with the explanation of pass by value given
previously in the course, but in fact it is consistent. The reference variable loc is
copied into the parameter c and cannot be changed by the method, but the
memory to which it refers is not copied and is under no such restriction. The
variable loc still refers to the same area of memory, but that area of memory
now contains different data. |
|