What exactly does "=" (equal) equal? A shallow copy?

R

raylopez99

In C++, you have symbolic overriding of "+", "=", etc, which C# also
has. This question is not really about that.

Rather, in C#, when you say:

MyObject X = new MyObject();
MyObject Y = new MyObject();

X = Y; //what does this '=' mean?

Is "X=Y" a shallow copy? I think it is.

So is X=Y above equivalent to using ICloneable to produce a shallow
copy clone ("return (this.MemberwiseClone());"), or is it more like a
deep copy ("return (BinaryFormatter1.Deserialize(memStream));")?

RL
 
J

Jesse Houwing

Hello raylopez99,
In C++, you have symbolic overriding of "+", "=", etc, which C# also
has. This question is not really about that.

Rather, in C#, when you say:

MyObject X = new MyObject();
MyObject Y = new MyObject();
X = Y; //what does this '=' mean?

Is "X=Y" a shallow copy? I think it is.

So is X=Y above equivalent to using ICloneable to produce a shallow
copy clone ("return (this.MemberwiseClone());"), or is it more like a
deep copy ("return (BinaryFormatter1.Deserialize(memStream));")?

RL

It's neither if MyObject is a class. in that case they become exactly the
same object. Not even a copy, truly the same.

If they're structs however you would get a sort of clone or shallow copy.
All references will just be copied. All value types will be cloned as well.

Jesse
 
B

Bill Butler

raylopez99 said:
In C++, you have symbolic overriding of "+", "=", etc, which C# also
has. This question is not really about that.

Rather, in C#, when you say:

MyObject X = new MyObject();
MyObject Y = new MyObject();

X = Y; //what does this '=' mean?

X is not an instance of MyObject; it is a reference to an instance of
MyObject. Basically X points to an instance.

X = Y // X now points to a different instance.
In C# you can reasign references and even declare null references (you
can get a null reference in C++, but that generally a design flaw)

The sample above would look like this in C++

MyObject *X = new MyObject();
MyObject *Y = new MyObject();

X = Y; //what does this '=' mean?
Simply reasigning a pointer (although it is also a memory leak in C++)

Is "X=Y" a shallow copy? I think it is.
Nope, simply a copy of a reference

In C++ you can do the following (Value semantics)
MyObject X; // X is an instance (on the stack)
MyObject Y; // Y is an instance (on the stack)
X=Y //Shallow copy from the second instance into the first instance

or you can do (reference semantics)

MyObject *X = new MyObject(); // X is a pointer to a instance of
MyObject (on the heap)
MyObject *Y = new MyObject(); // Y is a pointer to a instance of
MyObject (on the heap)

X = Y; // copy a pointer (memory leak)

So
MyObject X = new MyObject();
MyObject Y = new MyObject();

X = Y;

The only thing that is copied is the reference. Both X and Y point to
the same instance

Hope this helps
Bill
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

raylopez99 said:
In C++, you have symbolic overriding of "+", "=", etc, which C# also
has.

No, you can't overload the assignment operator in C#.
This question is not really about that.

Rather, in C#, when you say:

MyObject X = new MyObject();
MyObject Y = new MyObject();

X = Y; //what does this '=' mean?

It means that you copy the value of Y into X. The assignment operator
always does that, with no exception.

A class is a reference type, so the variables X and Y are references.
It's the reference that is copied, not the object. X becomes a reference
to the object that Y is referending.
Is "X=Y" a shallow copy? I think it is.

No, it's not. In .NET there is no automatic copying of objects. If you
want a copy of an object, you have to explicitly create one.
So is X=Y above equivalent to using ICloneable to produce a shallow
copy clone ("return (this.MemberwiseClone());"), or is it more like a
deep copy ("return (BinaryFormatter1.Deserialize(memStream));")?

Neither. It just copies the reference, not the object.
 
R

raylopez99

Yes Bill, it was very useful, since I come from a C++ background,
where "data slicing" or "alising" is always a problem and you have to
make copies of stuff.

I see that C# is more like "managed C++" in the CLI

Thank you

RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top