SendAPersonByValue(fred);
Console.WriteLine("\nAfter by value call, Person is:");
fred. Display();
Console.ReadLine();
}
Figure 4-13 shows the output of this call.
Figure 4-13.
Passing reference types by value locks the reference in place.
As you can see, the value of personAge has been modified. This behavior seems to fly in the face
of what it means to pass a parameter “by value.” Given that you were able to change the state of the
incoming Person, what was copied? The answer: a copy of the reference to the caller’s object. There-
fore, as the SendAPersonByValue() method is pointing to the same object as the caller, it is possible
to alter the object’s state data. What is not possible is to reassign what the reference is pointing to.
Do'stlaringiz bilan baham: |