// Passing ref-types by ref.
Console.WriteLine("\n***** Passing Person object by reference *****");
Person mel = new Person("Mel", 23);
Console.WriteLine("Before by ref call, Person is:");
mel.Display();
SendAPersonByReference(ref mel);
Console.WriteLine("After by ref call, Person is:");
mel.Display();
Console.ReadLine();
}
Figure 4-14.
Passing reference types by reference allows the reference to be redirected.
As you can see, an object named Mel returns after the call as a type named Nikki, as the
method was able to change what the incoming reference pointed to in memory. The golden rule to
keep in mind when passing reference types:
• If a reference type is passed by reference, the callee may change the values of the object’s
state data as well as the object it is referencing.
• If a reference type is passed by value, the callee may change the values of the object’s state
data but not the object it is referencing.
Do'stlaringiz bilan baham: |