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?