PART I
C h a p t e r 8 :
A C l o s e r L o o k a t M e t h o d s a n d C l a s s e s
175
PART IPART I
int a = 15, b = 20;
Console.WriteLine("a and b before call: " +
a + " " + b);
ob.NoChange(a, b);
Console.WriteLine("a and b after call: " +
a + " " + b);
}
}
The output from this program is shown here:
a and b before call: 15 20
a and b after call: 15 20
As you can see, the operations that occur inside
NoChange( )
have no effect on the values of
a
and
b
used in the call. Again, this is because
copies
of the
value
of
a
and
b
have been given
to parameters
i
and
j
, but
a
and
b
are otherwise completely independent of
i
and
j
. Thus,
assigning
i
a new value will not affect
a
.
When you pass a reference to a method, the situation is a bit more complicated. In this
case, the reference, itself, is still passed by value. Thus, a copy of the reference is made and
changes to the parameter will not affect the argument. (For example, making the parameter
refer to a new object will not change the object to which the argument refers.) However—
and this is a big however—changes
made to the object
being referred to by the parameter
will
affect the object referred to by the argument. Let’s see why.
Recall that when you create a variable of a class type, you are only creating a reference
to an object. Thus, when you pass this reference to a method, the parameter that receives it
will refer to the same object as that referred to by the argument. Therefore, the argument
and the parameter will both refer to the same object. This means that objects are passed to
methods by what is effectively call-by-reference. Thus, changes to the object inside the method
do
affect the object used as an argument. For example, consider the following program:
// Objects are passed by reference.
using System;
class Test {
public int a, b;
public Test(int i, int j) {
a = i;
b = j;
}
/* Pass an object. Now, ob.a and ob.b in object
used in the call will be changed. */
public void Change(Test ob) {
ob.a = ob.a + ob.b;
ob.b = -ob.b;
}
}
www.freepdf-books.com
176
P a r t I :
T h e C # L a n g u a g e
class CallByRef {
static void Main() {
Test ob = new Test(15, 20);
Console.WriteLine("ob.a and ob.b before call: " +
ob.a + " " + ob.b);
ob.Change(ob);
Console.WriteLine("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}
}
This program generates the following output:
ob.a and ob.b before call: 15 20
ob.a and ob.b after call: 35 -20
As you can see, in this case, the actions inside
Change( )
have affected the object used as an
argument.
To review: When a reference is passed to a method, the reference itself is passed by use
of call-by-value. Thus, a copy of that reference is made. However, the copy of that reference
will still refer to the same object as its corresponding argument. This means that objects are
implicitly passed using call-by-reference.
Do'stlaringiz bilan baham: |