Edward Diener <ediener@no_spam_incomm.comwrote:
Can one use 'ref' ( or 'out' ) on a reference type to create a reference
to a reference in C#. I know one can use it on a value type to create a
reference to that value.
Sure.
Both reference types and value types, when passed as parameters, are
passed by value - the difference is, with reference types, it's a
reference to the value (i.e. the object on the heap) that's passed by
value.
So, given the following, where Foo is a reference type:
void F(Foo x)
{
x = null; // ok because 'x' is a reference passed by value
}
When you understand this, it's obvious that ref / out are fine with
reference types. They simply cause the reference to the value on the
heap to be passed by reference itself. I.e.:
void F(out Foo x)
{
x = null; // affects caller
}
Foo a = new Foo();
F(out a);
Debug.Assert(a == null); // is true
-- Barry
--
http://barrkel.blogspot.com/