Search code examples
c#stackheap-memoryout-parameters

C# Out parameter question: How does Out handle value types?


UPDATE So totally pulled a tool moment. I really meant by reference versus Out/Ref. Anything that says 'ref' I really meant by reference as in

SomeMethod(Object someObject)

Versus

SomeMethod(out someObject)

Sorry. Just don't want to change the code so the answers already make sense.

Far as I understand, unlike ref where it "copies" the pointer and creates a new space on the stack to use that pointer, but won't change the pointer:

SomeMethod()
{
 SomeThing outer = new SomeThing();
 RefMethod(ref outer)
}

RefMethod(ref inner)  //new space on stack created and uses same pointer as outer
{
   inner.Hi = "There"; //updated the object being pointed to by outer
   inner = new SomeThing();//Given a new pointer, no longer shares pointer with outer
                           //New object on the heap
}

Out copies the pointer and can manipulate where it points to:

SomeMethod()
{
 SomeThing outer = new SomeThing();
 RefMethod(out outer)
}

RefMethod(out inner)  //same pointer shared
{

   inner = new SomeThing();//pointer now points to new place on heap  
                           //outer now points to new object
                           //Old object is orphaned if nothing else points to it
}

That's fine and dandy with objects, but what about value types seeing as they have nothing to point to being only on the stack?


Solution

  • Just because the variable lives on the stack (if it's a local variable) doesn't mean you can't create a pointer to it - and indeed that's the case with reference types as well.

    The pointer within RefMethod is to the "outer" variable - and the variable itself lives on the stack as it's an uncaptured local variable.

    As Leppie said, ref and out are identical except for the rules on definite assignment - in fact, the only difference in IL is an attribute applied to out parameters.

    See my article on parameter passing for more details about ref/out in general.