Understanding Parameter Direction for Reference Types


In the section "Declaring Functions with Parameters" I introduced you to the different directions that parameters support. There we discussed what it meant to pass primitive types, like int, double, byte, etc. through in parameters, ref parameters, or out parameters. I also told you that things were different for reference types and that we would discuss the differences at the end of the chapter. Promise kept!

Consider the code in Figure 2.91 . Take a close look at the highlighted lines. If you look at the function NameChild in the Parent class you will see that the function has an "in" parameter of type Child. The Child class has a public field called Name. The code in NameChild changes the Name field to "John." If you look down at the ProgramCode you will see that the child's name is first set to "Bill" before calling the function; the question is: what will the Name of the child be after calling NameChild? The answer is "John." Even though the Child parameter in NameChild is an "in" parameter you can still change the contents of the fields in the object. So what does it mean to have input parameters with reference types?

Figure 2.91 Even though ch is an "in" parameter in the NameChild function, you can still change the fields of the object ch points to and the changes are reflected to caller.
 class Child {    public string Name; } class Parent {    public void NameChild(Child ch)    {  ch.Name = "John";  } } class App {    void ProgramCode()    {      Child ch = new Child();      ch.Name = "Bill";      Parent pr = new Parent();      pr.NameChild(ch);      Response.Write(ch.Name);    } } 

Look at the code in Figure 2.92 . If you look at the highlighted line you will see that now the code in NameChild attempts to change the object the ch variable is pointing to. The question now is, after the call, will the original ch be pointing to the original child object or the one that NameChild is creating? The answer is that the original ch points to the original object, not the new one. What if the parameter were a ref parameter? Then in that case the original ch would point to the new object.

Figure 2.92 With an "in" parameter, when the code sets the ch variable to a different object from the caller's object, the caller's variable is unaffected.
 class Child {    public string Name; } class Parent {    public void NameChild(Child ch)    {  ch = new Child();  } } class App {    void ProgramCode()    {       Child ch = new Child();       Parent pr = new Parent();       pr.NameChild(ch);    } } 

With reference types, "in" means that the object the variable is pointing to can't be changed. With ref parameters, pointing the variable to a new object changes what the original variable is pointing to; the same is true for out parameters. Figure 2.93 shows the difference between in and ref parameters with reference types.

Figure 2.93. With "in" parameters, setting the variable in the function to a new object has no effect on the caller's variable. However, with ref parameters the caller's variable is also changed to point to the newly created object.

graphics/02fig93.gif



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net