JeremyJ wrote:
The two blocks of code do the same thing. I can do the equivilent in other languages and it works just fine (yes I have tested this).
They don't do entirely the same thing. What you want to do would actually be equivalent to this (sort of):
Recipient data;
Contact contact = data;
FetchContact(ref contact, row);
data = (Recipient)contact;
It's the final line that makes the difference. Let's look at the following example:
class Base { }
class Child1 : Base { }
class Child2 : Base { }
void FetchBase(ref Base base)
{
/* Omitted: do something with original value of base. */
base = new Child2();
}
Now if you do what you're describing above, it'd look like this:
Child1 c = new Child1();
FetchBase(ref (Base)c);
You'd end up trying to assign a Child2 object to a Child1 variable! It can't be done.
That's what Amotif was getting at.
JeremyJ wrote:
The only difference between the two blocks of code is the fact that I am creating a second pointer and passing that in. The Contact contact = ( Contact )data; is doing a shallow copy and not a deep copy.
No, the difference is that when FetchContact changes the value of the ref parameter, it's assigning to a variable of type Contact, whereas your first version would assign to a variable of type Recipient while it
thinks it's assigning to one of type Contact.
And there's no copying done at all, besides the reference. A shallow copy would be a new Recipient object but with the same members; and where they are references, the new object gets the same reference (a deep copy would also clone the referenced objects from the members).