How to NOT reference the same object??

  • Thread starter Thread starter WayneD
  • Start date Start date
W

WayneD

Hi All,
Just got started in C#...

Here's some C# code:

public MyClass

{

private MyThingy m_Thingy;



public SetThingy(MyThingy thingy)

{

m_Thingy = thingy;

}

}

I just found out that MyClass.m_Thingy will actually be referencing the same
MyThingy object that the caller passed in. Similar code in C++ would have
invoked the implied = operator and just copied the contents giving me a
separate, independent object with values initialized to what the passed in
object has. This is what I need, but I don't know how to make C# do the
same. How is this accomplished in C#?? I could always write a Copy
Constructor for MyThingy and do m_Thingy = new MyThingy(thingy) but I was
hoping there was another way so I don't have to write the copy constructor
in cases like this.

Thanks for any help,

Wayne
 
WayneD said:
Thanks for any help,

You might consider implementing the ICloneable interface in your
MyThingy class. Then you can create a copy like so:

MyThingy newThingy = oldThingy.Clone();

If your class has any member variables that are themselves reference
types, you'll need to make sure you copy those objects as well or
they'll be referenced by both instances.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
You can create a shallow copy this way:

class MyThingy
{
...
public MyThingy Copy()
{
return (MyThingy)this.MemberwiseClone();
}
}
 
Chris said:
You can create a shallow copy this way:

A shallow copy leads to exactly the same issue: reference types in both
copies point to the same objects.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
A shallow copy creates a new instance of the same type as the original
object, and then copies the nonstatic fields of the original object. If the
field is a value type, a bit-by-bit copy of the field is performed. If the
field is a reference type, the reference is copied but the referred object
is not; therefore, the reference in the original object and the reference in
the clone point to the same object. In contrast, a deep copy of an object
duplicates everything directly or indirectly referenced by the fields in the
object.
A shallow copy leads to exactly the same issue: reference types in both
copies point to the same objects.

He didn't indicate whether the fields would be reference types. I would
consider this "an issue", but "exactly the same issue"; the original
poster's problem was with regard to the host object itself referencing the
same object, and in the case of a shallow clone that is not the situation.

Jon
 
People have covered the other issues. A quick and cheap way to clone an
entire class' object graph would be to serialize the object and deserialize
it back into an instance. So long as every referred-to object is
serializable, you will get a true deep copy.

If you just want a shallow copy you can do the MemberwiseClone() as
previously suggested, or if value semantics are more appropriate for your
"MyThingy" type, you can define it instead as a struct, which will make a
copy (shallow) on assignment.

Richard
 
Just for the record, a shallow copy using MemberwiseClone will behave
identically to functionality that the default operator= will in C++. A
referenced object in the original object will also be referenced in the
copied object. So, if different behavior is desired, a method would need to
be created for either C# or for C++.

Thus, the original poster's question is answered as far as it went.

Chris R.
 
Back
Top